xref: /btstack/src/l2cap.c (revision 64e11ca9ae4068f217a511a6fddc3fed02590fbb)
143625864Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
3943625864Smatthias.ringwald  *  l2cap.c
4043625864Smatthias.ringwald  *
4143625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
4243625864Smatthias.ringwald  *
4343625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
4443625864Smatthias.ringwald  */
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include "l2cap.h"
47645658c9Smatthias.ringwald #include "hci.h"
482b3c6c9bSmatthias.ringwald #include "hci_dump.h"
4916ece135SMatthias Ringwald #include "btstack_debug.h"
500e2df43fSMatthias Ringwald #include "btstack_event.h"
51d3a9df87Smatthias.ringwald #include "btstack_memory.h"
5243625864Smatthias.ringwald 
5343625864Smatthias.ringwald #include <stdarg.h>
5443625864Smatthias.ringwald #include <string.h>
5543625864Smatthias.ringwald 
5643625864Smatthias.ringwald #include <stdio.h>
5743625864Smatthias.ringwald 
584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
604c744e21Smatthias.ringwald 
6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
62e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6339bda6d5Smatthias.ringwald 
6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6900d93d79Smatthias.ringwald 
705628cf69SMatthias Ringwald // internal table
715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
745628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
755628cf69SMatthias Ringwald 
7633c40538SMatthias Ringwald // prototypes
7733c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
7833c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
7933c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
8034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
8133c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
8233c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
8333c40538SMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8433c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
853d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
8630725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void);
87e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
88e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm);
89e7d0c9aaSMatthias Ringwald #endif
9033c40538SMatthias Ringwald 
915628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
925628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
932125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
945628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
955628cf69SMatthias Ringwald 
965628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
975628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
985628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
995628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
10039bda6d5Smatthias.ringwald 
10139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1022b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1032b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1042b83fb7dSmatthias.ringwald 
10533c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
10633c40538SMatthias Ringwald 
107fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
108fb37a842SMatthias Ringwald 
10933c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1105628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
11139bda6d5Smatthias.ringwald 
11234e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
11334e7e577SMatthias Ringwald     switch (index){
11434e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
11534e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
11634e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
11734e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
11834e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
11934e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
12034e7e577SMatthias Ringwald         default:
12134e7e577SMatthias Ringwald             return 0;
12234e7e577SMatthias Ringwald     }
12334e7e577SMatthias Ringwald }
1245628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1255628cf69SMatthias Ringwald     switch (channel_id){
1265628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1275628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1285628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1295628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1305628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1315628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1325628cf69SMatthias Ringwald         default:
1335628cf69SMatthias Ringwald             return -1;
1345628cf69SMatthias Ringwald         }
1355628cf69SMatthias Ringwald }
13639bda6d5Smatthias.ringwald 
13734e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
13834e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
13934e7e577SMatthias Ringwald     return 1;
14034e7e577SMatthias Ringwald }
14134e7e577SMatthias Ringwald 
14271de195eSMatthias Ringwald void l2cap_init(void){
1432b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
144808a48abSmatthias.ringwald 
145f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
146f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1477192e786SMatthias Ringwald     l2cap_le_services = NULL;
1487192e786SMatthias Ringwald     l2cap_le_channels = NULL;
149f5454fc6Smatthias.ringwald 
15033c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1515628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
152f5454fc6Smatthias.ringwald 
153ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
154ac301f95S[email protected] 
155fcadd0caSmatthias.ringwald     //
1562718e2e7Smatthias.ringwald     // register callback with HCI
157fcadd0caSmatthias.ringwald     //
158d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
159fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
160fb37a842SMatthias Ringwald 
161d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
162fb37a842SMatthias Ringwald 
16315a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
164fcadd0caSmatthias.ringwald }
165fcadd0caSmatthias.ringwald 
166ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
16733c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1681e6aba47Smatthias.ringwald }
1691e6aba47Smatthias.ringwald 
17017a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
17158de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
17258de5610Smatthias.ringwald }
17358de5610Smatthias.ringwald 
17458de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
175c9dc710bS[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",
176fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
177c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
178c9dc710bS[email protected]     uint8_t event[23];
17958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
18058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
18158de5610Smatthias.ringwald     event[2] = status;
182724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
183fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
184f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
185f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
186f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
187f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
188f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
189f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
19058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19117a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
19258de5610Smatthias.ringwald }
19358de5610Smatthias.ringwald 
19458de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
195e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
19658de5610Smatthias.ringwald     uint8_t event[4];
19758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
19858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
199f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
20058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20117a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
20258de5610Smatthias.ringwald }
20358de5610Smatthias.ringwald 
20458de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
205e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
206fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
20758de5610Smatthias.ringwald     uint8_t event[16];
20858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
20958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
210724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
211fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
212f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
213f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
214f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
21558de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21617a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2170af41d30Smatthias.ringwald }
218808a48abSmatthias.ringwald 
21934e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
22034e7e577SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
22133c40538SMatthias Ringwald     uint8_t event[4];
22233c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
22333c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
22434e7e577SMatthias Ringwald     little_endian_store_16(event, 2, channel);
22533c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22634e7e577SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
22733c40538SMatthias Ringwald }
22833c40538SMatthias Ringwald 
229fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
230ccf076adS[email protected]     uint8_t event[6];
231ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
232ccf076adS[email protected]     event[1] = 4;
233fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
234f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
235ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23633c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
23733c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
238ccf076adS[email protected] }
239ccf076adS[email protected] 
2407f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
241665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
242665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
243665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
244665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
245b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
246f62db1e3Smatthias.ringwald             return channel;
247f62db1e3Smatthias.ringwald         }
248f62db1e3Smatthias.ringwald     }
249f62db1e3Smatthias.ringwald     return NULL;
250f62db1e3Smatthias.ringwald }
251f62db1e3Smatthias.ringwald 
252e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
253e7d0c9aaSMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){
254e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_t it;
255e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
256e7d0c9aaSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
257e7d0c9aaSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
258e7d0c9aaSMatthias Ringwald         if ( channel->local_cid == local_cid) {
259e7d0c9aaSMatthias Ringwald             return channel;
260e7d0c9aaSMatthias Ringwald         }
261e7d0c9aaSMatthias Ringwald     }
262e7d0c9aaSMatthias Ringwald     return NULL;
263e7d0c9aaSMatthias Ringwald }
264e7d0c9aaSMatthias Ringwald #endif
265e7d0c9aaSMatthias Ringwald 
2665cb87675SMatthias Ringwald static l2cap_channel_t * l2cap_get_le_channel_for_address_and_addr_type(bd_addr_t address, bd_addr_type_t address_type){
2675cb87675SMatthias Ringwald     btstack_linked_list_iterator_t it;
2685cb87675SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
2695cb87675SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2705cb87675SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2715cb87675SMatthias Ringwald         if (channel->address_type != address_type)  continue;
2725cb87675SMatthias Ringwald         if (bd_addr_cmp(channel->address, address)) continue;
2735cb87675SMatthias Ringwald         return channel;
2745cb87675SMatthias Ringwald     }
2755cb87675SMatthias Ringwald     return NULL;
2765cb87675SMatthias Ringwald }
2775cb87675SMatthias Ringwald 
2780b9d7e78SMatthias Ringwald ///
2790b9d7e78SMatthias Ringwald 
28030725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
28130725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
28230725612SMatthias Ringwald     if (!channel) return;
28330725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
28430725612SMatthias Ringwald     l2cap_notify_channel_can_send();
28530725612SMatthias Ringwald }
28630725612SMatthias Ringwald 
2870b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
2880b9d7e78SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
2890b9d7e78SMatthias Ringwald     if (index < 0) return;
2900b9d7e78SMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
2910b9d7e78SMatthias Ringwald     l2cap_notify_channel_can_send();
2920b9d7e78SMatthias Ringwald }
2930b9d7e78SMatthias Ringwald 
2940b9d7e78SMatthias Ringwald ///
2950b9d7e78SMatthias Ringwald 
2966b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2976b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2986b1fde37Smatthias.ringwald     if (!channel) return 0;
2990b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
3007856fb31S[email protected] }
3017856fb31S[email protected] 
30283e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
30383e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
30483e7cdd9SMatthias Ringwald     if (!channel) return 0;
3050b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
30683e7cdd9SMatthias Ringwald }
30783e7cdd9SMatthias Ringwald 
308fc64f94aSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
3090b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
3102125de09SMatthias Ringwald }
3110b9d7e78SMatthias Ringwald 
3120b9d7e78SMatthias Ringwald ///
3133cab4fcaS[email protected] 
31496cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
31596cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
31696cbd662Smatthias.ringwald     if (channel) {
31796cbd662Smatthias.ringwald         return channel->remote_mtu;
31896cbd662Smatthias.ringwald     }
31996cbd662Smatthias.ringwald     return 0;
32096cbd662Smatthias.ringwald }
32196cbd662Smatthias.ringwald 
322ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
323665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
324665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
325665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
326665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3275932bd7cS[email protected]         if ( &channel->rtx == ts) {
3285932bd7cS[email protected]             return channel;
3295932bd7cS[email protected]         }
3305932bd7cS[email protected]     }
3315932bd7cS[email protected]     return NULL;
3325932bd7cS[email protected] }
3335932bd7cS[email protected] 
334ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3355932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
33695d2c8f4SMatthias Ringwald     if (!channel) return;
3375932bd7cS[email protected] 
3385932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3395932bd7cS[email protected] 
3405932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3415932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3425932bd7cS[email protected]     // notify client
3435932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3445932bd7cS[email protected] 
3455932bd7cS[email protected]     // discard channel
3469dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
347665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3485932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3495932bd7cS[email protected] }
3505932bd7cS[email protected] 
3515932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3525932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
353528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3545932bd7cS[email protected] }
3555932bd7cS[email protected] 
3565932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3575932bd7cS[email protected]     l2cap_stop_rtx(channel);
358cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
359528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
360528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
361528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3625932bd7cS[email protected] }
3635932bd7cS[email protected] 
3645932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3655932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3665932bd7cS[email protected]     l2cap_stop_rtx(channel);
367528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
368528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
369528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3705932bd7cS[email protected] }
3715932bd7cS[email protected] 
37271de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
373ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
374ac301f95S[email protected] }
375ac301f95S[email protected] 
376df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
377ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
378df3354fcS[email protected] }
3795932bd7cS[email protected] 
3807f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
381b1d43497Smatthias.ringwald 
382a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3839da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
384b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
385b1d43497Smatthias.ringwald     }
386b1d43497Smatthias.ringwald 
3879da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3882a373862S[email protected]     hci_reserve_packet_buffer();
389facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
39058de5610Smatthias.ringwald     va_list argptr;
39158de5610Smatthias.ringwald     va_start(argptr, identifier);
39270efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
39358de5610Smatthias.ringwald     va_end(argptr);
3949da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
395826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
39658de5610Smatthias.ringwald }
39758de5610Smatthias.ringwald 
398a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3997f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
40070efece1S[email protected] 
401a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4029da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
40370efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
40470efece1S[email protected]     }
40570efece1S[email protected] 
4069da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
4072a373862S[email protected]     hci_reserve_packet_buffer();
408facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
40970efece1S[email protected]     va_list argptr;
41070efece1S[email protected]     va_start(argptr, identifier);
41170efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
41270efece1S[email protected]     va_end(argptr);
4139da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
414826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
41570efece1S[email protected] }
41670efece1S[email protected] #endif
41770efece1S[email protected] 
418b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
419facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
420b1d43497Smatthias.ringwald }
4216218e6f1Smatthias.ringwald 
4226b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
4236b4af23dS[email protected]     return hci_reserve_packet_buffer();
4246b4af23dS[email protected] }
4256b4af23dS[email protected] 
42668a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
42768a0fcf7S[email protected]     hci_release_packet_buffer();
42868a0fcf7S[email protected] }
42968a0fcf7S[email protected] 
43068a0fcf7S[email protected] 
431b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
432b1d43497Smatthias.ringwald 
433c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
434c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
435c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
436c8b9416aS[email protected]     }
437c8b9416aS[email protected] 
43858de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
439b1d43497Smatthias.ringwald     if (!channel) {
4409da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
441b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4426218e6f1Smatthias.ringwald     }
4436218e6f1Smatthias.ringwald 
444fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4459da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
446a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
447a35252c8S[email protected]     }
448a35252c8S[email protected] 
449fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
450b1d43497Smatthias.ringwald 
451facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
452b1d43497Smatthias.ringwald 
453e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
454e9772277S[email protected] 
455e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
456fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14));
45758de5610Smatthias.ringwald     // 2 - ACL length
458f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
45958de5610Smatthias.ringwald     // 4 - L2CAP packet length
460f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
46158de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
462f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
46358de5610Smatthias.ringwald     // send
464826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
46591b99603Smatthias.ringwald 
4666218e6f1Smatthias.ringwald     return err;
46758de5610Smatthias.ringwald }
46858de5610Smatthias.ringwald 
469fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
4702149f12eSmatthias.ringwald 
471c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
472c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4732149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4742149f12eSmatthias.ringwald     }
4752149f12eSmatthias.ringwald 
476fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
477fc64f94aSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
478c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
479c8b9416aS[email protected]     }
480c8b9416aS[email protected] 
481fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
4822149f12eSmatthias.ringwald 
483facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4842149f12eSmatthias.ringwald 
485e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
486e9772277S[email protected] 
487e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
488fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
4892149f12eSmatthias.ringwald     // 2 - ACL length
490f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4912149f12eSmatthias.ringwald     // 4 - L2CAP packet length
492f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4932149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
494f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4952149f12eSmatthias.ringwald     // send
496826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4972149f12eSmatthias.ringwald 
4982149f12eSmatthias.ringwald     return err;
4992149f12eSmatthias.ringwald }
5002149f12eSmatthias.ringwald 
501ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
502b1d43497Smatthias.ringwald 
503a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
504a35252c8S[email protected]     if (!channel) {
505ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
506a35252c8S[email protected]         return -1;   // TODO: define error
507a35252c8S[email protected]     }
508a35252c8S[email protected] 
509f0efaa57S[email protected]     if (len > channel->remote_mtu){
510ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
511f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
512f0efaa57S[email protected]     }
513f0efaa57S[email protected] 
514fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
515ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
516b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
517b1d43497Smatthias.ringwald     }
518b1d43497Smatthias.ringwald 
5192a373862S[email protected]     hci_reserve_packet_buffer();
520facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
521b1d43497Smatthias.ringwald 
522b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
523b1d43497Smatthias.ringwald 
524b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
525b1d43497Smatthias.ringwald }
526b1d43497Smatthias.ringwald 
527fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
5282149f12eSmatthias.ringwald 
529fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
530ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
5312149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
5322149f12eSmatthias.ringwald     }
5332149f12eSmatthias.ringwald 
5342a373862S[email protected]     hci_reserve_packet_buffer();
535facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
5362149f12eSmatthias.ringwald 
5372149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
5382149f12eSmatthias.ringwald 
539fc64f94aSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
5402149f12eSmatthias.ringwald }
5412149f12eSmatthias.ringwald 
542fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
543fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5440e37e417S[email protected] }
5450e37e417S[email protected] 
54628ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
54728ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
54828ca2b46S[email protected] }
54928ca2b46S[email protected] 
55028ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
55128ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
55228ca2b46S[email protected] }
55328ca2b46S[email protected] 
55428ca2b46S[email protected] 
555b1d43497Smatthias.ringwald 
5568158c421Smatthias.ringwald // MARK: L2CAP_RUN
5572cd0be45Smatthias.ringwald // process outstanding signaling tasks
5587f02f414SMatthias Ringwald static void l2cap_run(void){
5592b83fb7dSmatthias.ringwald 
56022c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
56122c29ab4SMatthias Ringwald 
5622b83fb7dSmatthias.ringwald     // check pending signaling responses
5632b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5642b83fb7dSmatthias.ringwald 
5652b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
566a35252c8S[email protected] 
567a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
568a35252c8S[email protected] 
5692b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5702b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
57163a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
572f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5732b83fb7dSmatthias.ringwald 
574f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
575f53da564S[email protected]         signaling_responses_pending--;
576f53da564S[email protected]         int i;
577f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
578f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
579f53da564S[email protected]         }
580f53da564S[email protected] 
581f53da564S[email protected]         switch (response_code){
5822b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5832b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5842bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5854d816277S[email protected]                 if (result == 0x0003){
5862bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5874d816277S[email protected]                 }
5882b360848Smatthias.ringwald                 break;
5892b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5902b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5912b83fb7dSmatthias.ringwald                 break;
5922b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5933b0484b3S[email protected]                 switch (infoType){
5943b0484b3S[email protected]                     case 1: { // Connectionless MTU
5953b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5963b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5973b0484b3S[email protected]                         break;
5983b0484b3S[email protected]                     }
5993b0484b3S[email protected]                     case 2: { // Extended Features Supported
600462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
601462e630dS[email protected]                         uint32_t features = 0x280;
6023b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
6033b0484b3S[email protected]                         break;
6043b0484b3S[email protected]                     }
6053b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
6063b0484b3S[email protected]                         uint8_t map[8];
6073b0484b3S[email protected]                         memset(map, 0, 8);
608288636a2SMatthias Ringwald                         map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
6093b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
6103b0484b3S[email protected]                         break;
6113b0484b3S[email protected]                     }
6123b0484b3S[email protected]                     default:
6132b83fb7dSmatthias.ringwald                         // all other types are not supported
6142b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
6153b0484b3S[email protected]                         break;
6162b83fb7dSmatthias.ringwald                 }
6172b83fb7dSmatthias.ringwald                 break;
61863a7246aSmatthias.ringwald             case COMMAND_REJECT:
6195ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
620a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
62170efece1S[email protected]             case COMMAND_REJECT_LE:
62270efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
62363a7246aSmatthias.ringwald                 break;
62470efece1S[email protected] #endif
6252b83fb7dSmatthias.ringwald             default:
6262b83fb7dSmatthias.ringwald                 // should not happen
6272b83fb7dSmatthias.ringwald                 break;
6282b83fb7dSmatthias.ringwald         }
6292b83fb7dSmatthias.ringwald     }
6302b83fb7dSmatthias.ringwald 
631ae280e73Smatthias.ringwald     uint8_t  config_options[4];
632665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
633665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
634665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
635baf94f06S[email protected] 
636665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
63722c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6382cd0be45Smatthias.ringwald         switch (channel->state){
6392cd0be45Smatthias.ringwald 
640df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
641ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
642fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
643a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
644ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
645fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
646ad671560S[email protected]                 }
647ad671560S[email protected]                 break;
648ad671560S[email protected] 
64902b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
650baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
65164472d52Smatthias.ringwald                 // send connection request - set state first
65264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
65302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6548f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
65502b22dc4Smatthias.ringwald                 break;
65602b22dc4Smatthias.ringwald 
657e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
658fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
65922c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
660fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
661e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6629dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
663665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
664d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
665e7ff783cSmatthias.ringwald                 break;
666e7ff783cSmatthias.ringwald 
667552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
668fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
669fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
67028ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
671fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
672552d92a1Smatthias.ringwald                 break;
673552d92a1Smatthias.ringwald 
6746fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
675fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6766fdcc387Smatthias.ringwald                 // success, start l2cap handshake
677b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6786fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
679fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6805932bd7cS[email protected]                 l2cap_start_rtx(channel);
6816fdcc387Smatthias.ringwald                 break;
6826fdcc387Smatthias.ringwald 
683fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
684fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
68573cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
68663a7246aSmatthias.ringwald                     uint16_t flags = 0;
68728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
68863a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
68963a7246aSmatthias.ringwald                         flags = 1;
69063a7246aSmatthias.ringwald                     } else {
69128ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
69263a7246aSmatthias.ringwald                     }
69363a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
694fc64f94aSMatthias 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);
69563a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
69663a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
69763a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
698f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
699fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
70063a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
70163a7246aSmatthias.ringwald                     } else {
702fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
70363a7246aSmatthias.ringwald                     }
70463a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
705fa8473a4Smatthias.ringwald                 }
70673cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
70728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
70828ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
709b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
710ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
711ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
712f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
713fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
7145932bd7cS[email protected]                     l2cap_start_rtx(channel);
715fa8473a4Smatthias.ringwald                 }
716fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
717552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
718552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
719fa8473a4Smatthias.ringwald                 }
720552d92a1Smatthias.ringwald                 break;
721552d92a1Smatthias.ringwald 
722e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
723fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
72422c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
725fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7265932bd7cS[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 :)
727756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
728e7ff783cSmatthias.ringwald                 break;
729e7ff783cSmatthias.ringwald 
730e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
731fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
732b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7332cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
734fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7352cd0be45Smatthias.ringwald                 break;
7362cd0be45Smatthias.ringwald             default:
7372cd0be45Smatthias.ringwald                 break;
7382cd0be45Smatthias.ringwald         }
7392cd0be45Smatthias.ringwald     }
740da886c03S[email protected] 
741a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
742efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
743efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
744efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
745efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
746efedfb4cSMatthias Ringwald         switch (channel->state){
7475cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7485cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7495cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
7505cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
7511b8b8d05SMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7525cb87675SMatthias 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);
7535cb87675SMatthias Ringwald                 break;
75423017473SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
75523017473SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
75623017473SMatthias Ringwald                 // TODO: support larger MPS
75723017473SMatthias Ringwald                 channel->state = L2CAP_STATE_OPEN;
75823017473SMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, channel->local_mtu, 23, channel->credits_incoming, 0);
759*64e11ca9SMatthias Ringwald                 // notify client
760*64e11ca9SMatthias Ringwald                 l2cap_emit_channel_opened(channel, 0);
76123017473SMatthias Ringwald                 break;
762e7d0c9aaSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
763e7d0c9aaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
764e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
765e7d0c9aaSMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, channel->reason);
766e7d0c9aaSMatthias Ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
767e7d0c9aaSMatthias Ringwald                 l2cap_stop_rtx(channel);
768e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
769e7d0c9aaSMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
770e7d0c9aaSMatthias Ringwald                 break;
771efedfb4cSMatthias Ringwald             default:
772efedfb4cSMatthias Ringwald                 break;
773efedfb4cSMatthias Ringwald         }
774efedfb4cSMatthias Ringwald     }
775efedfb4cSMatthias Ringwald 
776da886c03S[email protected]     // send l2cap con paramter update if necessary
777da886c03S[email protected]     hci_connections_get_iterator(&it);
778665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
779665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7805d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
781b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
782da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
783b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
784b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
785b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
786b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
787b68d7bc3SMatthias Ringwald                 break;
788da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
789b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
790b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
791da886c03S[email protected]                 break;
792da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
793b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
794b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
795da886c03S[email protected]                 break;
796da886c03S[email protected]             default:
797da886c03S[email protected]                 break;
798da886c03S[email protected]         }
799efedfb4cSMatthias Ringwald #if 0
800efedfb4cSMatthias Ringwald void l2cap_le_run(void){
801efedfb4cSMatthias Ringwald     for all channels
802efedfb4cSMatthias Ringwald         switch(state):
803efedfb4cSMatthias Ringwald 
804efedfb4cSMatthias Ringwald     if outgoing transfer active
805efedfb4cSMatthias Ringwald         send next chunk
806efedfb4cSMatthias Ringwald         if done, notify app
807efedfb4cSMatthias Ringwald }
808efedfb4cSMatthias Ringwald #endif
809da886c03S[email protected]     }
8104d7157c3S[email protected] #endif
811da886c03S[email protected] 
81222c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
8132cd0be45Smatthias.ringwald }
8142cd0be45Smatthias.ringwald 
8154aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
8164ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
817fa8c92f6Smatthias.ringwald }
818fa8c92f6Smatthias.ringwald 
81971de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
8204ff786cfS[email protected]     return l2cap_max_mtu();
821e5e1518dS[email protected] }
822e5e1518dS[email protected] 
823fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
8242df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
8255533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
8262df5dadcS[email protected]         // success, start l2cap handshake
827fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
8282df5dadcS[email protected]         // check remote SSP feature first
8292df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
8302df5dadcS[email protected]     }
8312df5dadcS[email protected] }
8322df5dadcS[email protected] 
833efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE
8345cb87675SMatthias Ringwald static void l2cap_handle_le_connection_complete(l2cap_channel_t * channel, uint8_t status, hci_con_handle_t con_handle){
835efedfb4cSMatthias Ringwald     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
836efedfb4cSMatthias Ringwald         log_info("l2cap_le_handle_connection_complete expected state");
8375cb87675SMatthias Ringwald         // TODO: bail if status != 0
8385cb87675SMatthias Ringwald 
839efedfb4cSMatthias Ringwald         // success, start l2cap handshake
840efedfb4cSMatthias Ringwald         channel->con_handle = con_handle;
8415cb87675SMatthias Ringwald         channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
842efedfb4cSMatthias Ringwald     }
843efedfb4cSMatthias Ringwald }
844efedfb4cSMatthias Ringwald #endif
845efedfb4cSMatthias Ringwald 
846efedfb4cSMatthias Ringwald 
8472df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
8482df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
8492df5dadcS[email protected] 
8502df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
851ac301f95S[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));
852fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
8532df5dadcS[email protected]         // request security level 2
8542df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
855fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
8562df5dadcS[email protected]         return;
8572df5dadcS[email protected]     }
8582df5dadcS[email protected]     // fine, go ahead
8592df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
8602df5dadcS[email protected] }
8612df5dadcS[email protected] 
862da144af5SMatthias 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,
863da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
864da144af5SMatthias Ringwald 
865da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
866da144af5SMatthias Ringwald     if (!channel) {
867da144af5SMatthias Ringwald         return NULL;
868da144af5SMatthias Ringwald     }
869da144af5SMatthias Ringwald 
870da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
871da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
872da144af5SMatthias Ringwald 
873da144af5SMatthias Ringwald     // fill in
874da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
875da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
876da144af5SMatthias Ringwald     channel->address_type = address_type;
877da144af5SMatthias Ringwald     channel->psm = psm;
878da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
879da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
880da144af5SMatthias Ringwald     channel->required_security_level = security_level;
881da144af5SMatthias Ringwald 
882da144af5SMatthias Ringwald     //
883da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
884da144af5SMatthias Ringwald     channel->con_handle = 0;
885da144af5SMatthias Ringwald 
886da144af5SMatthias Ringwald     // set initial state
887da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
888da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
889da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
890da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
891da144af5SMatthias Ringwald     return channel;
892da144af5SMatthias Ringwald }
893da144af5SMatthias Ringwald 
8949077cb15SMatthias Ringwald /**
8959077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
8969077cb15SMatthias Ringwald  * @param packet_handler
8979077cb15SMatthias Ringwald  * @param address
8989077cb15SMatthias Ringwald  * @param psm
8999077cb15SMatthias Ringwald  * @param mtu
9009077cb15SMatthias Ringwald  * @param local_cid
9019077cb15SMatthias Ringwald  */
9029077cb15SMatthias Ringwald 
903da144af5SMatthias 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){
904da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
905da144af5SMatthias Ringwald 
906da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
907da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
908da144af5SMatthias Ringwald     }
909da144af5SMatthias Ringwald 
910da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
911fc64f94aSMatthias Ringwald     if (!channel) {
9129077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
9139077cb15SMatthias Ringwald     }
9149077cb15SMatthias Ringwald 
9159077cb15SMatthias Ringwald     // add to connections list
916fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
9179077cb15SMatthias Ringwald 
9189077cb15SMatthias Ringwald     // store local_cid
9199077cb15SMatthias Ringwald     if (out_local_cid){
920fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
9219077cb15SMatthias Ringwald     }
9229077cb15SMatthias Ringwald 
9239077cb15SMatthias Ringwald     // check if hci connection is already usable
9249077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
9259077cb15SMatthias Ringwald     if (conn){
926add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
927fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
9289077cb15SMatthias Ringwald         // check if remote supported fearures are already received
9299077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
930fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
9319077cb15SMatthias Ringwald         }
9329077cb15SMatthias Ringwald     }
9339077cb15SMatthias Ringwald 
9349077cb15SMatthias Ringwald     l2cap_run();
9359077cb15SMatthias Ringwald 
9369077cb15SMatthias Ringwald     return 0;
9379077cb15SMatthias Ringwald }
9389077cb15SMatthias Ringwald 
939ce8f182eSMatthias Ringwald void
940ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
941e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
942b35f641cSmatthias.ringwald     // find channel for local_cid
943b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
944f62db1e3Smatthias.ringwald     if (channel) {
945e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
946f62db1e3Smatthias.ringwald     }
9472cd0be45Smatthias.ringwald     // process
9482cd0be45Smatthias.ringwald     l2cap_run();
94943625864Smatthias.ringwald }
9501e6aba47Smatthias.ringwald 
951afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
952665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
953665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
954665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
955665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
956058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
957c22aecc9S[email protected]         // channel for this address found
958c22aecc9S[email protected]         switch (channel->state){
959c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
960c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
961afde0c52Smatthias.ringwald                 // failure, forward error code
962afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
963afde0c52Smatthias.ringwald                 // discard channel
9649dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
965665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
966d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
967c22aecc9S[email protected]                 break;
968c22aecc9S[email protected]             default:
969c22aecc9S[email protected]                 break;
970afde0c52Smatthias.ringwald         }
971afde0c52Smatthias.ringwald     }
972afde0c52Smatthias.ringwald }
973afde0c52Smatthias.ringwald 
974afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
975665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
976665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
977665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
978665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
979058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
9802df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
981afde0c52Smatthias.ringwald         }
982afde0c52Smatthias.ringwald     }
9836fdcc387Smatthias.ringwald     // process
9846fdcc387Smatthias.ringwald     l2cap_run();
985afde0c52Smatthias.ringwald }
986b448a0e7Smatthias.ringwald 
98733c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
98833c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
98933c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
99033c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
99133c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
99233c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
993fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
99433c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
99534e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
99633c40538SMatthias Ringwald     }
9972125de09SMatthias Ringwald 
9982125de09SMatthias Ringwald     int i;
9992125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
1000773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
10012125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
100234e7e577SMatthias Ringwald         int can_send;
100334e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
100434e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
100534e7e577SMatthias Ringwald         } else {
100634e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
100734e7e577SMatthias Ringwald         }
100834e7e577SMatthias Ringwald         if (!can_send) continue;
100934e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
101034e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
10112125de09SMatthias Ringwald     }
101233c40538SMatthias Ringwald }
101333c40538SMatthias Ringwald 
1014d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
1015afde0c52Smatthias.ringwald 
1016afde0c52Smatthias.ringwald     bd_addr_t address;
1017afde0c52Smatthias.ringwald     hci_con_handle_t handle;
1018665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
10192d00edd4Smatthias.ringwald     int hci_con_used;
1020afde0c52Smatthias.ringwald 
10210e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1022afde0c52Smatthias.ringwald 
1023afde0c52Smatthias.ringwald         // handle connection complete events
1024afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1025724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
1026afde0c52Smatthias.ringwald             if (packet[2] == 0){
1027f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
1028afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
1029afde0c52Smatthias.ringwald             } else {
1030afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
1031afde0c52Smatthias.ringwald             }
1032afde0c52Smatthias.ringwald             break;
1033afde0c52Smatthias.ringwald 
1034afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1035afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1036073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1037afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1038724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1039afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1040afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
104103cfbabcSmatthias.ringwald                 }
10421e6aba47Smatthias.ringwald             }
104339d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
104439d59809Smatthias.ringwald             break;
104539d59809Smatthias.ringwald 
104639d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
104739d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
1048afde0c52Smatthias.ringwald             break;
104927a923d0Smatthias.ringwald 
10501e6aba47Smatthias.ringwald         // handle disconnection complete events
1051afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1052c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
1053f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1054665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1055665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1056665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1057fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
105815ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
10599dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1060665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1061d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
106227a923d0Smatthias.ringwald             }
1063991fea48SMatthias Ringwald #ifdef ENABLE_BLE
1064991fea48SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1065991fea48SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1066991fea48SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1067991fea48SMatthias Ringwald                 if (channel->con_handle != handle) continue;
1068991fea48SMatthias Ringwald                 l2cap_emit_channel_closed(channel);
1069991fea48SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1070991fea48SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
1071991fea48SMatthias Ringwald             }
1072991fea48SMatthias Ringwald #endif
1073afde0c52Smatthias.ringwald             break;
1074fcadd0caSmatthias.ringwald 
107533c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
107663fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
10776218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
107802b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
107933c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
10806218e6f1Smatthias.ringwald             break;
10816218e6f1Smatthias.ringwald 
1082ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
1083afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1084f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1085bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
108680ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
10872d00edd4Smatthias.ringwald             hci_con_used = 0;
1088665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1089665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1090665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1091fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10922d00edd4Smatthias.ringwald                 hci_con_used = 1;
1093c22aecc9S[email protected]                 break;
1094ee091cf1Smatthias.ringwald             }
10952d00edd4Smatthias.ringwald             if (hci_con_used) break;
1096d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
10979edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1098afde0c52Smatthias.ringwald             break;
1099ee091cf1Smatthias.ringwald 
1100df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1101f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1102665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1103665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1104665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1105fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11062df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1107df3354fcS[email protected]                 break;
1108df3354fcS[email protected]             }
1109c22aecc9S[email protected]             break;
1110df3354fcS[email protected] 
11115cb87675SMatthias Ringwald #ifdef ENABLE_BLE
11125cb87675SMatthias Ringwald         case HCI_EVENT_LE_META:
11135cb87675SMatthias Ringwald             switch (packet[2]){
11145cb87675SMatthias Ringwald                 int addr_type;
11155cb87675SMatthias Ringwald                 l2cap_channel_t * channel;
11165cb87675SMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
11175cb87675SMatthias Ringwald                     // Connection management
11185cb87675SMatthias Ringwald                     reverse_bd_addr(&packet[8], address);
11195cb87675SMatthias Ringwald                     addr_type = (bd_addr_type_t)packet[7];
11205cb87675SMatthias Ringwald                     log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address));
11215cb87675SMatthias Ringwald                     // get l2cap_channel
11225cb87675SMatthias Ringwald                     // also pass in status
11235cb87675SMatthias Ringwald                     channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type);
11245cb87675SMatthias Ringwald                     if (!channel) break;
11255cb87675SMatthias Ringwald                     l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4));
11265cb87675SMatthias Ringwald                     break;
11275cb87675SMatthias Ringwald                 default:
11285cb87675SMatthias Ringwald                     break;
11295cb87675SMatthias Ringwald             }
11305cb87675SMatthias Ringwald             break;
11315cb87675SMatthias Ringwald #endif
11325cb87675SMatthias Ringwald 
11335611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1134f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1135bd63148eS[email protected]             log_info("l2cap - security level update");
1136665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1137665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1138665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1139fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11405533f01eS[email protected] 
1141bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1142bd63148eS[email protected] 
1143e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
11445533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
11455533f01eS[email protected] 
1146df3354fcS[email protected]                 switch (channel->state){
1147df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
11485533f01eS[email protected]                         if (actual_level >= required_level){
1149f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1150f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
11511eb2563eS[email protected]                         } else {
1152775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
11531eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11541eb2563eS[email protected]                         }
1155df3354fcS[email protected]                         break;
1156df3354fcS[email protected] 
1157df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
11585533f01eS[email protected]                         if (actual_level >= required_level){
1159df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1160df3354fcS[email protected]                         } else {
1161df3354fcS[email protected]                             // disconnnect, authentication not good enough
1162df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1163df3354fcS[email protected]                         }
1164df3354fcS[email protected]                         break;
1165df3354fcS[email protected] 
1166df3354fcS[email protected]                     default:
1167df3354fcS[email protected]                         break;
1168df3354fcS[email protected]                 }
1169f85a9399S[email protected]             }
1170f85a9399S[email protected]             break;
1171f85a9399S[email protected] 
1172afde0c52Smatthias.ringwald         default:
1173afde0c52Smatthias.ringwald             break;
1174afde0c52Smatthias.ringwald     }
1175afde0c52Smatthias.ringwald 
1176bd63148eS[email protected]     l2cap_run();
11771e6aba47Smatthias.ringwald }
11781e6aba47Smatthias.ringwald 
1179afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1180b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1181e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1182e7ff783cSmatthias.ringwald     l2cap_run();
118384836b65Smatthias.ringwald }
118484836b65Smatthias.ringwald 
11852b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
11864cf56b4aSmatthias.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."
11872b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
11882b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
11892b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
11902b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
11912b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
11922b360848Smatthias.ringwald         signaling_responses_pending++;
11932b360848Smatthias.ringwald         l2cap_run();
11942b360848Smatthias.ringwald     }
11952b360848Smatthias.ringwald }
11962b360848Smatthias.ringwald 
1197b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1198645658c9Smatthias.ringwald 
11999da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1200645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1201645658c9Smatthias.ringwald     if (!service) {
1202645658c9Smatthias.ringwald         // 0x0002 PSM not supported
12032b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1204645658c9Smatthias.ringwald         return;
1205645658c9Smatthias.ringwald     }
1206645658c9Smatthias.ringwald 
12075061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1208645658c9Smatthias.ringwald     if (!hci_connection) {
12092b360848Smatthias.ringwald         //
12109da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1211645658c9Smatthias.ringwald         return;
1212645658c9Smatthias.ringwald     }
12132bd8b7e7S[email protected] 
1214645658c9Smatthias.ringwald     // alloc structure
12159da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1216da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1217da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
12182b360848Smatthias.ringwald     if (!channel){
12192b360848Smatthias.ringwald         // 0x0004 No resources available
12202b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
12212b360848Smatthias.ringwald         return;
12222b360848Smatthias.ringwald     }
1223da144af5SMatthias Ringwald 
1224fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1225b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1226b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1227645658c9Smatthias.ringwald 
1228f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
12292985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
12302985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
12319775e25bSmatthias.ringwald     }
12329775e25bSmatthias.ringwald 
1233645658c9Smatthias.ringwald     // set initial state
1234df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1235ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1236e405ae81Smatthias.ringwald 
1237645658c9Smatthias.ringwald     // add to connections list
1238665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1239645658c9Smatthias.ringwald 
1240f85a9399S[email protected]     // assert security requirements
12411eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1242e405ae81Smatthias.ringwald }
1243645658c9Smatthias.ringwald 
1244ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1245e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1246b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1247e405ae81Smatthias.ringwald     if (!channel) {
1248ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1249e405ae81Smatthias.ringwald         return;
1250e405ae81Smatthias.ringwald     }
1251e405ae81Smatthias.ringwald 
1252552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1253e405ae81Smatthias.ringwald 
1254552d92a1Smatthias.ringwald     // process
1255552d92a1Smatthias.ringwald     l2cap_run();
1256e405ae81Smatthias.ringwald }
1257645658c9Smatthias.ringwald 
12587ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
12597ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1260b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1261e405ae81Smatthias.ringwald     if (!channel) {
1262ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1263e405ae81Smatthias.ringwald         return;
1264e405ae81Smatthias.ringwald     }
1265e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
12667ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1267e7ff783cSmatthias.ringwald     l2cap_run();
1268645658c9Smatthias.ringwald }
1269645658c9Smatthias.ringwald 
12707f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1271b1988dceSmatthias.ringwald 
1272b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1273b1988dceSmatthias.ringwald 
1274f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
127563a7246aSmatthias.ringwald     if (flags & 1) {
127663a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
127763a7246aSmatthias.ringwald     }
127863a7246aSmatthias.ringwald 
12792784b77dSmatthias.ringwald     // accept the other's configuration options
1280f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
12813de7c0caSmatthias.ringwald     uint16_t pos     = 8;
12823de7c0caSmatthias.ringwald     while (pos < end_pos){
128363a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
128463a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
128563a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
128663a7246aSmatthias.ringwald         pos++;
12871dc511deSmatthias.ringwald         uint8_t length = command[pos++];
12881dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
128963a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1290f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
12919da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
129263a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
129363a7246aSmatthias.ringwald         }
12940fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
12950fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1296f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
12970fe7a9d0S[email protected]         }
129863a7246aSmatthias.ringwald         // check for unknown options
129963a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1300c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
130163a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
13021dc511deSmatthias.ringwald         }
13031dc511deSmatthias.ringwald         pos += length;
13041dc511deSmatthias.ringwald     }
13052784b77dSmatthias.ringwald }
13062784b77dSmatthias.ringwald 
1307fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
13089da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
130973cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
131073cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1311019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1312019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1313fa8473a4Smatthias.ringwald     return 1;
1314fa8473a4Smatthias.ringwald }
1315fa8473a4Smatthias.ringwald 
1316fa8473a4Smatthias.ringwald 
13177f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
13181e6aba47Smatthias.ringwald 
131900d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
132000d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
132138e5900eSmatthias.ringwald     uint16_t result = 0;
13221e6aba47Smatthias.ringwald 
13239da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1324b35f641cSmatthias.ringwald 
13259a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
13269a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
13279a011532Smatthias.ringwald         switch (channel->state){
1328fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
13299a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
13302b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
13319a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
13329a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
13339a011532Smatthias.ringwald                 break;
13349a011532Smatthias.ringwald 
13359a011532Smatthias.ringwald             default:
13369a011532Smatthias.ringwald                 // ignore in other states
13379a011532Smatthias.ringwald                 break;
13389a011532Smatthias.ringwald         }
13399a011532Smatthias.ringwald         return;
13409a011532Smatthias.ringwald     }
13419a011532Smatthias.ringwald 
134256081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
13431e6aba47Smatthias.ringwald     switch (channel->state) {
13441e6aba47Smatthias.ringwald 
13451e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
13461e6aba47Smatthias.ringwald             switch (code){
13471e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
13485932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1349f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
135038e5900eSmatthias.ringwald                     switch (result) {
135138e5900eSmatthias.ringwald                         case 0:
1352169f8b28Smatthias.ringwald                             // successful connection
1353f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1354fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
135528ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
135638e5900eSmatthias.ringwald                             break;
135738e5900eSmatthias.ringwald                         case 1:
13585932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
13595932bd7cS[email protected]                             l2cap_start_ertx(channel);
136038e5900eSmatthias.ringwald                             break;
136138e5900eSmatthias.ringwald                         default:
1362eb920dbeSmatthias.ringwald                             // channel closed
1363eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1364f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
136538e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1366eb920dbeSmatthias.ringwald 
1367eb920dbeSmatthias.ringwald                             // drop link key if security block
1368eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
136915a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1370eb920dbeSmatthias.ringwald                             }
1371eb920dbeSmatthias.ringwald 
1372eb920dbeSmatthias.ringwald                             // discard channel
1373665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1374d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
137538e5900eSmatthias.ringwald                             break;
13761e6aba47Smatthias.ringwald                     }
13771e6aba47Smatthias.ringwald                     break;
137838e5900eSmatthias.ringwald 
137938e5900eSmatthias.ringwald                 default:
13801e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
138138e5900eSmatthias.ringwald                     break;
13821e6aba47Smatthias.ringwald             }
13831e6aba47Smatthias.ringwald             break;
13841e6aba47Smatthias.ringwald 
1385fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1386f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1387ae280e73Smatthias.ringwald             switch (code) {
1388ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
138928ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1390ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
139163a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
139263a7246aSmatthias.ringwald                         // only done if continuation not set
139363a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
139463a7246aSmatthias.ringwald                     }
1395ae280e73Smatthias.ringwald                     break;
13961e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
13975932bd7cS[email protected]                     l2cap_stop_rtx(channel);
13985932bd7cS[email protected]                     switch (result){
13995932bd7cS[email protected]                         case 0: // success
14005932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
14015932bd7cS[email protected]                             break;
14025932bd7cS[email protected]                         case 4: // pending
14035932bd7cS[email protected]                             l2cap_start_ertx(channel);
14045932bd7cS[email protected]                             break;
14055932bd7cS[email protected]                         default:
1406fe9d8984S[email protected]                             // retry on negative result
1407fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1408fe9d8984S[email protected]                             break;
1409fe9d8984S[email protected]                     }
14105a67bd4aSmatthias.ringwald                     break;
14115a67bd4aSmatthias.ringwald                 default:
14125a67bd4aSmatthias.ringwald                     break;
14131e6aba47Smatthias.ringwald             }
1414fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1415fa8473a4Smatthias.ringwald                 // for open:
14165a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1417fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1418c8e4258aSmatthias.ringwald             }
1419c8e4258aSmatthias.ringwald             break;
1420f62db1e3Smatthias.ringwald 
1421f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1422f62db1e3Smatthias.ringwald             switch (code) {
1423f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
142427a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
142527a923d0Smatthias.ringwald                     break;
14265a67bd4aSmatthias.ringwald                 default:
14275a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
14285a67bd4aSmatthias.ringwald                     break;
142927a923d0Smatthias.ringwald             }
143027a923d0Smatthias.ringwald             break;
143184836b65Smatthias.ringwald 
143284836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
143384836b65Smatthias.ringwald             // @TODO handle incoming requests
143484836b65Smatthias.ringwald             break;
143584836b65Smatthias.ringwald 
143684836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
143784836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
143884836b65Smatthias.ringwald             break;
143910642e45Smatthias.ringwald         default:
144010642e45Smatthias.ringwald             break;
144127a923d0Smatthias.ringwald     }
14429da54300S[email protected]     // log_info("new state %u", channel->state);
144327a923d0Smatthias.ringwald }
144427a923d0Smatthias.ringwald 
144500d93d79Smatthias.ringwald 
14467f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
144700d93d79Smatthias.ringwald 
144800d93d79Smatthias.ringwald     // get code, signalind identifier and command len
144900d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
145000d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
145100d93d79Smatthias.ringwald 
145200d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
145300d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
145463a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
145500d93d79Smatthias.ringwald         return;
145600d93d79Smatthias.ringwald     }
145700d93d79Smatthias.ringwald 
145800d93d79Smatthias.ringwald     // general commands without an assigned channel
145900d93d79Smatthias.ringwald     switch(code) {
146000d93d79Smatthias.ringwald 
146100d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1462f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1463f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
146400d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
14652b83fb7dSmatthias.ringwald             return;
146600d93d79Smatthias.ringwald         }
146700d93d79Smatthias.ringwald 
14682b360848Smatthias.ringwald         case ECHO_REQUEST:
14692b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
14702b83fb7dSmatthias.ringwald             return;
147100d93d79Smatthias.ringwald 
147200d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1473f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
14742b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
14752b83fb7dSmatthias.ringwald             return;
147600d93d79Smatthias.ringwald         }
147700d93d79Smatthias.ringwald 
147800d93d79Smatthias.ringwald         default:
147900d93d79Smatthias.ringwald             break;
148000d93d79Smatthias.ringwald     }
148100d93d79Smatthias.ringwald 
148200d93d79Smatthias.ringwald 
148300d93d79Smatthias.ringwald     // Get potential destination CID
1484f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
148500d93d79Smatthias.ringwald 
148600d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1487665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1488665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1489665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1490665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1491fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
149200d93d79Smatthias.ringwald         if (code & 1) {
1493b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1494b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
149500d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
14964e32727eSmatthias.ringwald                 break;
149700d93d79Smatthias.ringwald             }
149800d93d79Smatthias.ringwald         } else {
1499b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
150000d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
150100d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15024e32727eSmatthias.ringwald                 break;
150300d93d79Smatthias.ringwald             }
150400d93d79Smatthias.ringwald         }
150500d93d79Smatthias.ringwald     }
150600d93d79Smatthias.ringwald }
150700d93d79Smatthias.ringwald 
1508e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1509c48b2a2cSMatthias Ringwald // @returns valid
1510c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1511e7d0c9aaSMatthias Ringwald     hci_connection_t * connection;
1512c48b2a2cSMatthias Ringwald     uint16_t result;
1513c48b2a2cSMatthias Ringwald     uint8_t  event[10];
1514e7d0c9aaSMatthias Ringwald     uint16_t le_psm;
1515e7d0c9aaSMatthias Ringwald     l2cap_service_t * service;
15161b8b8d05SMatthias Ringwald     l2cap_channel_t * channel;
15171b8b8d05SMatthias Ringwald     btstack_linked_list_iterator_t it;
151800d93d79Smatthias.ringwald 
15191b8b8d05SMatthias Ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
152023017473SMatthias Ringwald     log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id);
15211b8b8d05SMatthias Ringwald 
15221b8b8d05SMatthias Ringwald     switch (code){
152300d93d79Smatthias.ringwald 
1524c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1525c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1526ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1527ccf076adS[email protected]             break;
1528da886c03S[email protected] 
1529c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1530e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1531da886c03S[email protected]             if (connection){
15326d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
15336d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1534c48b2a2cSMatthias Ringwald                     return 0;
15356d91fb6cSMatthias Ringwald                 }
1536da886c03S[email protected]                 int update_parameter = 1;
1537a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
15384ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1539c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1540c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1541c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1542c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1543da886c03S[email protected] 
1544da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1545da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1546da886c03S[email protected] 
1547da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1548da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1549da886c03S[email protected] 
1550da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1551da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1552da886c03S[email protected] 
1553da886c03S[email protected]                 if (update_parameter){
1554da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1555da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1556da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1557da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1558da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1559da886c03S[email protected]                 } else {
1560da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1561da886c03S[email protected]                 }
1562c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1563da886c03S[email protected]             }
1564da886c03S[email protected] 
156533c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1566c48b2a2cSMatthias Ringwald 
1567c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1568c48b2a2cSMatthias Ringwald             event[1] = 8;
1569c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1570c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
157133c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1572ccf076adS[email protected]             break;
1573c48b2a2cSMatthias Ringwald 
1574e7d0c9aaSMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_REQUEST:
1575e7d0c9aaSMatthias Ringwald 
1576e7d0c9aaSMatthias Ringwald             // get hci connection, bail if not found (must not happen)
1577e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1578e7d0c9aaSMatthias Ringwald             if (!connection) return 0;
1579e7d0c9aaSMatthias Ringwald 
1580e7d0c9aaSMatthias Ringwald             // check if service registered
1581e7d0c9aaSMatthias Ringwald             le_psm  = little_endian_read_16(command, 4);
1582e7d0c9aaSMatthias Ringwald             service = l2cap_le_get_service(le_psm);
1583e7d0c9aaSMatthias Ringwald 
1584e7d0c9aaSMatthias Ringwald             if (service){
1585e7d0c9aaSMatthias Ringwald 
1586e7d0c9aaSMatthias Ringwald                 uint16_t source_cid = little_endian_read_16(command, 6);
1587e7d0c9aaSMatthias Ringwald                 if (source_cid < 0x40){
1588e7d0c9aaSMatthias Ringwald                     // 0x0009 Connection refused - Invalid Source CID
1589e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009);
1590e7d0c9aaSMatthias Ringwald                     return 1;
1591e7d0c9aaSMatthias Ringwald                 }
1592e7d0c9aaSMatthias Ringwald 
1593e7d0c9aaSMatthias Ringwald                 // go through list of channels for this ACL connection and check if we get a match
1594e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1595e7d0c9aaSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it)){
15961b8b8d05SMatthias Ringwald                     l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
15971b8b8d05SMatthias Ringwald                     if (a_channel->con_handle != handle) continue;
15981b8b8d05SMatthias Ringwald                     if (a_channel->remote_cid != source_cid) continue;
1599e7d0c9aaSMatthias Ringwald                     // 0x000a Connection refused - Source CID already allocated
1600e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a);
1601e7d0c9aaSMatthias Ringwald                     return 1;
1602e7d0c9aaSMatthias Ringwald                 }
1603e7d0c9aaSMatthias Ringwald 
1604e7d0c9aaSMatthias Ringwald                 // TODO: deal with authentication requirements, errors 0x005 - 000x8
1605e7d0c9aaSMatthias Ringwald 
1606e7d0c9aaSMatthias Ringwald                 // allocate channel
16071b8b8d05SMatthias Ringwald                 channel = l2cap_create_channel_entry(service->packet_handler, connection->address,
1608e7d0c9aaSMatthias Ringwald                     BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
1609e7d0c9aaSMatthias Ringwald                 if (!channel){
1610e7d0c9aaSMatthias Ringwald                     // 0x0004 Connection refused – no resources available
1611e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004);
1612e7d0c9aaSMatthias Ringwald                     return 1;
1613e7d0c9aaSMatthias Ringwald                 }
1614e7d0c9aaSMatthias Ringwald 
1615e7d0c9aaSMatthias Ringwald                 channel->con_handle = handle;
1616e7d0c9aaSMatthias Ringwald                 channel->remote_cid = source_cid;
1617e7d0c9aaSMatthias Ringwald                 channel->remote_sig_id = sig_id;
1618e7d0c9aaSMatthias Ringwald 
1619e7d0c9aaSMatthias Ringwald                 // limit local mtu to max acl packet length - l2cap header
1620e7d0c9aaSMatthias Ringwald                 if (channel->local_mtu > l2cap_max_le_mtu()) {
1621e7d0c9aaSMatthias Ringwald                     channel->local_mtu = l2cap_max_le_mtu();
1622e7d0c9aaSMatthias Ringwald                 }
1623e7d0c9aaSMatthias Ringwald 
1624e7d0c9aaSMatthias Ringwald                 // set initial state
1625e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1626e7d0c9aaSMatthias Ringwald 
1627e7d0c9aaSMatthias Ringwald                 // add to connections list
1628e7d0c9aaSMatthias Ringwald                 btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1629e7d0c9aaSMatthias Ringwald 
1630e7d0c9aaSMatthias Ringwald                 // post connection request event
1631e7d0c9aaSMatthias Ringwald                 l2cap_emit_connection_request(channel);
1632e7d0c9aaSMatthias Ringwald 
1633e7d0c9aaSMatthias Ringwald             } else {
1634e7d0c9aaSMatthias Ringwald                 // Connection refused – LE_PSM not supported
1635e7d0c9aaSMatthias Ringwald                 l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002);
1636e7d0c9aaSMatthias Ringwald             }
1637e7d0c9aaSMatthias Ringwald             break;
16381b8b8d05SMatthias Ringwald 
16391b8b8d05SMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_RESPONSE:
16401b8b8d05SMatthias Ringwald             // Find channel for this sig_id and connection handle
16411b8b8d05SMatthias Ringwald             channel = NULL;
16421b8b8d05SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
16431b8b8d05SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
16441b8b8d05SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16451b8b8d05SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
16461b8b8d05SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
16471b8b8d05SMatthias Ringwald                 channel = a_channel;
16481b8b8d05SMatthias Ringwald                 break;
16491b8b8d05SMatthias Ringwald             }
16501b8b8d05SMatthias Ringwald             // TODO: send error
16511b8b8d05SMatthias Ringwald             if (!channel) break;
16521b8b8d05SMatthias Ringwald 
16531b8b8d05SMatthias Ringwald             // cid + 0
16541b8b8d05SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
16551b8b8d05SMatthias Ringwald             if (result){
16561b8b8d05SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
16571b8b8d05SMatthias Ringwald                 // map l2cap connection response result to BTstack status enumeration
16581b8b8d05SMatthias Ringwald                 l2cap_emit_channel_opened(channel, result);
16591b8b8d05SMatthias Ringwald 
16601b8b8d05SMatthias Ringwald                 // discard channel
16611b8b8d05SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
16621b8b8d05SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
16631b8b8d05SMatthias Ringwald                 break;
16641b8b8d05SMatthias Ringwald             }
16651b8b8d05SMatthias Ringwald 
16661b8b8d05SMatthias Ringwald             // success
16671b8b8d05SMatthias Ringwald             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
16681b8b8d05SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
16691b8b8d05SMatthias Ringwald             channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
16701b8b8d05SMatthias Ringwald             channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
16711b8b8d05SMatthias Ringwald             channel->state = L2CAP_STATE_OPEN;
16721b8b8d05SMatthias Ringwald             l2cap_emit_channel_opened(channel, result);
16731b8b8d05SMatthias Ringwald             break;
16741b8b8d05SMatthias Ringwald 
1675c48b2a2cSMatthias Ringwald         default:
1676c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1677c48b2a2cSMatthias Ringwald             return 0;
1678ccf076adS[email protected]     }
1679c48b2a2cSMatthias Ringwald     return 1;
1680c48b2a2cSMatthias Ringwald }
1681e7d0c9aaSMatthias Ringwald #endif
1682c48b2a2cSMatthias Ringwald 
1683c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
1684c48b2a2cSMatthias Ringwald 
1685c48b2a2cSMatthias Ringwald     // Get Channel ID
1686c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1687c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1688c48b2a2cSMatthias Ringwald 
1689c48b2a2cSMatthias Ringwald     switch (channel_id) {
1690c48b2a2cSMatthias Ringwald 
1691c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1692c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1693c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1694c48b2a2cSMatthias Ringwald                 // handle signaling commands
1695c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1696c48b2a2cSMatthias Ringwald 
1697c48b2a2cSMatthias Ringwald                 // increment command_offset
1698c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1699c48b2a2cSMatthias Ringwald             }
1700c48b2a2cSMatthias Ringwald             break;
1701c48b2a2cSMatthias Ringwald         }
1702c48b2a2cSMatthias Ringwald 
1703e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1704e7d0c9aaSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE: {
1705e7d0c9aaSMatthias Ringwald             uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1706e7d0c9aaSMatthias Ringwald             int      valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1707c48b2a2cSMatthias Ringwald             if (!valid){
17081bbc0b23S[email protected]                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
1709ccf076adS[email protected]             }
1710ccf076adS[email protected]             break;
1711e7d0c9aaSMatthias Ringwald         }
1712e7d0c9aaSMatthias Ringwald #endif
1713c48b2a2cSMatthias Ringwald 
1714c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1715c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1716c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1717ccf076adS[email protected]             }
1718c48b2a2cSMatthias Ringwald             break;
1719c48b2a2cSMatthias Ringwald 
1720c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1721c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1722c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1723c48b2a2cSMatthias Ringwald             }
1724c48b2a2cSMatthias Ringwald             break;
1725c48b2a2cSMatthias Ringwald 
1726c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1727c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1728c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1729c48b2a2cSMatthias Ringwald             }
1730c48b2a2cSMatthias Ringwald             break;
17311bbc0b23S[email protected] 
17325652b5ffS[email protected]         default: {
173300d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
1734*64e11ca9SMatthias Ringwald             l2cap_channel_t * l2cap_channel;
1735*64e11ca9SMatthias Ringwald             l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1736d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
17373d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
173800d93d79Smatthias.ringwald             }
1739*64e11ca9SMatthias Ringwald #ifdef ENABLE_BLE
1740*64e11ca9SMatthias Ringwald             l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id);
1741*64e11ca9SMatthias Ringwald             if (l2cap_channel) {
1742*64e11ca9SMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1743*64e11ca9SMatthias Ringwald             }
1744*64e11ca9SMatthias Ringwald #endif
17455652b5ffS[email protected]             break;
17465652b5ffS[email protected]         }
17475652b5ffS[email protected]     }
174800d93d79Smatthias.ringwald 
17491eb2563eS[email protected]     l2cap_run();
17502718e2e7Smatthias.ringwald }
175100d93d79Smatthias.ringwald 
175215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
175327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1754f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1755f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1756f62db1e3Smatthias.ringwald     // discard channel
17579dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1758665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1759d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1760c8e4258aSmatthias.ringwald }
17611e6aba47Smatthias.ringwald 
17628f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1763665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1764665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1765665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1766665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
17679d9bbc01Smatthias.ringwald         if ( service->psm == psm){
17689d9bbc01Smatthias.ringwald             return service;
17699d9bbc01Smatthias.ringwald         };
17709d9bbc01Smatthias.ringwald     }
17719d9bbc01Smatthias.ringwald     return NULL;
17729d9bbc01Smatthias.ringwald }
17739d9bbc01Smatthias.ringwald 
17747192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
17757192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
17767192e786SMatthias Ringwald }
17777192e786SMatthias Ringwald 
1778e0abb8e7S[email protected] 
1779be2053a6SMatthias 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){
1780be2053a6SMatthias Ringwald 
1781be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1782e0abb8e7S[email protected] 
17834bb582b6Smatthias.ringwald     // check for alread registered psm
17849d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1785277abc2cSmatthias.ringwald     if (service) {
1786be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1787be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1788277abc2cSmatthias.ringwald     }
17899d9bbc01Smatthias.ringwald 
17904bb582b6Smatthias.ringwald     // alloc structure
1791bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1792277abc2cSmatthias.ringwald     if (!service) {
1793be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1794be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1795277abc2cSmatthias.ringwald     }
17969d9bbc01Smatthias.ringwald 
17979d9bbc01Smatthias.ringwald     // fill in
17989d9bbc01Smatthias.ringwald     service->psm = psm;
17999d9bbc01Smatthias.ringwald     service->mtu = mtu;
180005ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1801df3354fcS[email protected]     service->required_security_level = security_level;
18029d9bbc01Smatthias.ringwald 
18039d9bbc01Smatthias.ringwald     // add to services list
1804665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1805c0e866bfSmatthias.ringwald 
1806c0e866bfSmatthias.ringwald     // enable page scan
180715a95bd5SMatthias Ringwald     gap_connectable_control(1);
18085842b6d9Smatthias.ringwald 
1809be2053a6SMatthias Ringwald     return 0;
18109d9bbc01Smatthias.ringwald }
18119d9bbc01Smatthias.ringwald 
18127e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
1813e0abb8e7S[email protected] 
1814e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1815e0abb8e7S[email protected] 
18169d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
18177e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1818665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1819d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1820c0e866bfSmatthias.ringwald 
1821c0e866bfSmatthias.ringwald     // disable page scan when no services registered
18227e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
182315a95bd5SMatthias Ringwald         gap_connectable_control(0);
18249d9bbc01Smatthias.ringwald     }
18257e8856ebSMatthias Ringwald     return 0;
18267e8856ebSMatthias Ringwald }
18279d9bbc01Smatthias.ringwald 
18285652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
182905ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
18305628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
18315628cf69SMatthias Ringwald     if (index < 0) return;
18325628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
18335652b5ffS[email protected] }
18345652b5ffS[email protected] 
1835a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
18367192e786SMatthias Ringwald 
1837e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
1838e7d0c9aaSMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, le_psm);
18397192e786SMatthias Ringwald }
1840efedfb4cSMatthias Ringwald 
1841da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
18427192e786SMatthias Ringwald 
1843da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
18447192e786SMatthias Ringwald 
18457192e786SMatthias Ringwald     // check for alread registered psm
18467192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
18477192e786SMatthias Ringwald     if (service) {
1848da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
18497192e786SMatthias Ringwald     }
18507192e786SMatthias Ringwald 
18517192e786SMatthias Ringwald     // alloc structure
18527192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
18537192e786SMatthias Ringwald     if (!service) {
18547192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
1855da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
18567192e786SMatthias Ringwald     }
18577192e786SMatthias Ringwald 
18587192e786SMatthias Ringwald     // fill in
18597192e786SMatthias Ringwald     service->psm = psm;
1860da144af5SMatthias Ringwald     service->mtu = 0;
18617192e786SMatthias Ringwald     service->packet_handler = packet_handler;
18627192e786SMatthias Ringwald     service->required_security_level = security_level;
18637192e786SMatthias Ringwald 
18647192e786SMatthias Ringwald     // add to services list
1865665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
18667192e786SMatthias Ringwald 
18677192e786SMatthias Ringwald     // done
1868da144af5SMatthias Ringwald     return 0;
18697192e786SMatthias Ringwald }
18707192e786SMatthias Ringwald 
1871da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
18727192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
18737192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
18749367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1875da144af5SMatthias Ringwald 
1876665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
18777192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
1878da144af5SMatthias Ringwald     return 0;
18797192e786SMatthias Ringwald }
1880da144af5SMatthias Ringwald 
1881da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
1882e7d0c9aaSMatthias Ringwald     // get channel
1883e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
1884e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
1885e7d0c9aaSMatthias Ringwald 
1886e7d0c9aaSMatthias Ringwald     // validate state
1887e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
1888e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1889e7d0c9aaSMatthias Ringwald     }
1890e7d0c9aaSMatthias Ringwald 
1891efedfb4cSMatthias Ringwald     // set state accept connection
189223017473SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
189323017473SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
189423017473SMatthias Ringwald     channel->local_mtu = mtu;
189523017473SMatthias Ringwald     channel->credits_incoming = initial_credits;
1896e7d0c9aaSMatthias Ringwald 
1897e7d0c9aaSMatthias Ringwald     // go
1898e7d0c9aaSMatthias Ringwald     l2cap_run();
1899da144af5SMatthias Ringwald     return 0;
1900da144af5SMatthias Ringwald }
1901da144af5SMatthias Ringwald 
1902da144af5SMatthias Ringwald /**
1903da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
1904da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1905da144af5SMatthias Ringwald  */
1906da144af5SMatthias Ringwald 
1907da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
1908e7d0c9aaSMatthias Ringwald     // get channel
1909e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
1910e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
1911e7d0c9aaSMatthias Ringwald 
1912e7d0c9aaSMatthias Ringwald     // validate state
1913e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
1914e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1915e7d0c9aaSMatthias Ringwald     }
1916e7d0c9aaSMatthias Ringwald 
1917efedfb4cSMatthias Ringwald     // set state decline connection
1918e7d0c9aaSMatthias Ringwald     channel->state  = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
1919e7d0c9aaSMatthias Ringwald     channel->reason = 0x04; // no resources available
1920e7d0c9aaSMatthias Ringwald     l2cap_run();
1921da144af5SMatthias Ringwald     return 0;
1922da144af5SMatthias Ringwald }
1923da144af5SMatthias Ringwald 
1924da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
1925da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
1926efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
1927efedfb4cSMatthias Ringwald 
1928da144af5SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
1929da144af5SMatthias Ringwald 
19305cb87675SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, LEVEL_0);
1931da144af5SMatthias Ringwald     if (!channel) {
1932da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1933da144af5SMatthias Ringwald     }
1934e7d0c9aaSMatthias Ringwald     log_info("l2cap_le_create_channel %p", channel);
1935da144af5SMatthias Ringwald 
1936da144af5SMatthias Ringwald     // store local_cid
1937da144af5SMatthias Ringwald     if (out_local_cid){
1938da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
1939da144af5SMatthias Ringwald     }
1940da144af5SMatthias Ringwald 
1941e7d0c9aaSMatthias Ringwald     channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
1942e7d0c9aaSMatthias Ringwald 
1943efedfb4cSMatthias Ringwald     // add to connections list
1944efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1945efedfb4cSMatthias Ringwald 
1946da144af5SMatthias Ringwald     // check if hci connection is already usable
1947da144af5SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type);
1948efedfb4cSMatthias Ringwald 
1949da144af5SMatthias Ringwald     if (conn){
1950da144af5SMatthias Ringwald         log_info("l2cap_le_create_channel, hci connection already exists");
19515cb87675SMatthias Ringwald         l2cap_handle_le_connection_complete(channel, 0, conn->con_handle);
1952efedfb4cSMatthias Ringwald     } else {
1953efedfb4cSMatthias Ringwald 
1954efedfb4cSMatthias Ringwald         //
1955efedfb4cSMatthias Ringwald         // TODO: start timer
1956efedfb4cSMatthias Ringwald         // ..
1957efedfb4cSMatthias Ringwald 
1958efedfb4cSMatthias Ringwald         // start connection
1959efedfb4cSMatthias Ringwald         uint8_t res = gap_auto_connection_start(address_type, address);
196048af0c56SMatthias Ringwald         if (res){
1961efedfb4cSMatthias Ringwald             // discard channel object
1962e7d0c9aaSMatthias Ringwald             log_info("gap_auto_connection_start failed! 0x%02x", res);
1963e7d0c9aaSMatthias Ringwald             btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1964efedfb4cSMatthias Ringwald             btstack_memory_l2cap_channel_free(channel);
1965efedfb4cSMatthias Ringwald             return res;
1966da144af5SMatthias Ringwald         }
1967efedfb4cSMatthias Ringwald     }
1968da144af5SMatthias Ringwald     return 0;
1969da144af5SMatthias Ringwald }
1970da144af5SMatthias Ringwald 
1971da144af5SMatthias Ringwald /**
1972da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
1973da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1974da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
1975da144af5SMatthias Ringwald  */
1976*64e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
1977e7d0c9aaSMatthias Ringwald     // get channel
1978efedfb4cSMatthias Ringwald     // bail if missing
1979efedfb4cSMatthias Ringwald     // check state
1980efedfb4cSMatthias Ringwald     // check incoming credits + credits <= 0xffff
1981efedfb4cSMatthias Ringwald     // set credits_granted
1982efedfb4cSMatthias Ringwald     // l2cap_le_run()
1983da144af5SMatthias Ringwald     return 0;
1984da144af5SMatthias Ringwald }
1985da144af5SMatthias Ringwald 
1986da144af5SMatthias Ringwald /**
1987da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
1988da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1989da144af5SMatthias Ringwald  */
1990*64e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){
1991efedfb4cSMatthias Ringwald     // check if data can be sent via le at all
1992da144af5SMatthias Ringwald     return 0;
1993da144af5SMatthias Ringwald }
1994da144af5SMatthias Ringwald 
1995da144af5SMatthias Ringwald /**
1996da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
1997da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
1998da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
1999da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2000da144af5SMatthias Ringwald  */
2001*64e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
2002efedfb4cSMatthias Ringwald     // same as for non-le
2003da144af5SMatthias Ringwald     return 0;
2004da144af5SMatthias Ringwald }
2005da144af5SMatthias Ringwald 
2006da144af5SMatthias Ringwald /**
2007da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
2008da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
2009da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2010da144af5SMatthias Ringwald  * @param data                  data to send
2011da144af5SMatthias Ringwald  * @param size                  data size
2012da144af5SMatthias Ringwald  */
2013*64e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
2014*64e11ca9SMatthias Ringwald 
2015*64e11ca9SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2016*64e11ca9SMatthias Ringwald     if (!channel) {
2017*64e11ca9SMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2018*64e11ca9SMatthias Ringwald         return -1;   // TODO: define error
2019*64e11ca9SMatthias Ringwald     }
2020*64e11ca9SMatthias Ringwald 
2021*64e11ca9SMatthias Ringwald     if (len > channel->remote_mtu){
2022*64e11ca9SMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
2023*64e11ca9SMatthias Ringwald         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
2024*64e11ca9SMatthias Ringwald     }
2025*64e11ca9SMatthias Ringwald 
2026*64e11ca9SMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
2027*64e11ca9SMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
2028*64e11ca9SMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2029*64e11ca9SMatthias Ringwald     }
2030*64e11ca9SMatthias Ringwald 
2031*64e11ca9SMatthias Ringwald     hci_reserve_packet_buffer();
2032*64e11ca9SMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
2033*64e11ca9SMatthias Ringwald 
2034*64e11ca9SMatthias Ringwald     memcpy(&acl_buffer[8], data, len);
2035*64e11ca9SMatthias Ringwald 
2036*64e11ca9SMatthias Ringwald     return l2cap_send_prepared_connectionless(channel->con_handle, local_cid, len);
2037da144af5SMatthias Ringwald }
2038da144af5SMatthias Ringwald 
2039da144af5SMatthias Ringwald /**
2040da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
2041da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2042da144af5SMatthias Ringwald  */
2043da144af5SMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t cid)
2044da144af5SMatthias Ringwald {
2045efedfb4cSMatthias Ringwald     // find channel
2046efedfb4cSMatthias Ringwald     // check state
2047efedfb4cSMatthias Ringwald     // set state SEND_DISCONNECT
2048da144af5SMatthias Ringwald     return 0;
2049da144af5SMatthias Ringwald }
2050da144af5SMatthias Ringwald 
20517f02f414SMatthias Ringwald #endif
2052