xref: /btstack/src/l2cap.c (revision 0e2df43f5cbae3fc71139523458b98f30307d21b)
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"
50*0e2df43fSMatthias 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);
8533c40538SMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size );
8633c40538SMatthias Ringwald 
875628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
885628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
892125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
905628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
915628cf69SMatthias Ringwald 
925628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
935628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
945628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
955628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
9639bda6d5Smatthias.ringwald 
9739bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
982b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
992b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1002b83fb7dSmatthias.ringwald 
10133c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
10233c40538SMatthias Ringwald 
103fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
104fb37a842SMatthias Ringwald 
10533c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1065628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
10739bda6d5Smatthias.ringwald 
10834e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
10934e7e577SMatthias Ringwald     switch (index){
11034e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
11134e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
11234e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
11334e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
11434e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
11534e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
11634e7e577SMatthias Ringwald         default:
11734e7e577SMatthias Ringwald             return 0;
11834e7e577SMatthias Ringwald     }
11934e7e577SMatthias Ringwald }
1205628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1215628cf69SMatthias Ringwald     switch (channel_id){
1225628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1235628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1245628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1255628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1265628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1275628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1285628cf69SMatthias Ringwald         default:
1295628cf69SMatthias Ringwald             return -1;
1305628cf69SMatthias Ringwald         }
1315628cf69SMatthias Ringwald }
13239bda6d5Smatthias.ringwald 
13334e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
13434e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
13534e7e577SMatthias Ringwald     return 1;
13634e7e577SMatthias Ringwald }
13734e7e577SMatthias Ringwald 
13871de195eSMatthias Ringwald void l2cap_init(void){
1392b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
140808a48abSmatthias.ringwald 
141f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
142f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1437192e786SMatthias Ringwald     l2cap_le_services = NULL;
1447192e786SMatthias Ringwald     l2cap_le_channels = NULL;
145f5454fc6Smatthias.ringwald 
14633c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1475628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
148f5454fc6Smatthias.ringwald 
149ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
150ac301f95S[email protected] 
151fcadd0caSmatthias.ringwald     //
1522718e2e7Smatthias.ringwald     // register callback with HCI
153fcadd0caSmatthias.ringwald     //
154d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
155fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
156fb37a842SMatthias Ringwald 
157d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
158fb37a842SMatthias Ringwald 
15915a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
160fcadd0caSmatthias.ringwald }
161fcadd0caSmatthias.ringwald 
162ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
16333c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1641e6aba47Smatthias.ringwald }
1651e6aba47Smatthias.ringwald 
16617a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
16758de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
16858de5610Smatthias.ringwald }
16958de5610Smatthias.ringwald 
17058de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
171c9dc710bS[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",
172fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
173c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
174c9dc710bS[email protected]     uint8_t event[23];
17558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
17658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
17758de5610Smatthias.ringwald     event[2] = status;
178724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
179fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
180f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
181f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
182f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
183f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
184f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
185f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
18658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18717a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
18833c40538SMatthias Ringwald 
18933c40538SMatthias Ringwald     // if channel opened successfully, also send can send now if possible
19033c40538SMatthias Ringwald     if (status) return;
191fc64f94aSMatthias Ringwald     if (hci_can_send_acl_packet_now(channel->con_handle)){
19234e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
19333c40538SMatthias Ringwald     } else {
19433c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
19533c40538SMatthias Ringwald     }
19658de5610Smatthias.ringwald }
19758de5610Smatthias.ringwald 
19858de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
199e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
20058de5610Smatthias.ringwald     uint8_t event[4];
20158de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
20258de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
203f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
20458de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20517a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
20658de5610Smatthias.ringwald }
20758de5610Smatthias.ringwald 
20858de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
209e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
210fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
21158de5610Smatthias.ringwald     uint8_t event[16];
21258de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
21358de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
214724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
215fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
216f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
217f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
218f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
21958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22017a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2210af41d30Smatthias.ringwald }
222808a48abSmatthias.ringwald 
22334e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
22434e7e577SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
22533c40538SMatthias Ringwald     uint8_t event[4];
22633c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
22733c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
22834e7e577SMatthias Ringwald     little_endian_store_16(event, 2, channel);
22933c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23034e7e577SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
23133c40538SMatthias Ringwald }
23233c40538SMatthias Ringwald 
233fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
234ccf076adS[email protected]     uint8_t event[6];
235ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
236ccf076adS[email protected]     event[1] = 4;
237fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
238f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
239ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24033c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
24133c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
242ccf076adS[email protected] }
243ccf076adS[email protected] 
2447f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
245665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
246665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
247665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
248665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
249b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
250f62db1e3Smatthias.ringwald             return channel;
251f62db1e3Smatthias.ringwald         }
252f62db1e3Smatthias.ringwald     }
253f62db1e3Smatthias.ringwald     return NULL;
254f62db1e3Smatthias.ringwald }
255f62db1e3Smatthias.ringwald 
2566b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2576b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2586b1fde37Smatthias.ringwald     if (!channel) return 0;
259fc64f94aSMatthias Ringwald     int can_send = hci_can_send_acl_packet_now(channel->con_handle);
26033c40538SMatthias Ringwald     if (!can_send){
26133c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
26233c40538SMatthias Ringwald     }
26333c40538SMatthias Ringwald     return can_send;
2647856fb31S[email protected] }
2657856fb31S[email protected] 
26683e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
26783e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
26883e7cdd9SMatthias Ringwald     if (!channel) return 0;
269fc64f94aSMatthias Ringwald     int can_send = hci_can_send_prepared_acl_packet_now(channel->con_handle);
27033c40538SMatthias Ringwald     if (!can_send){
27133c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
27233c40538SMatthias Ringwald     }
27333c40538SMatthias Ringwald     return can_send;
27483e7cdd9SMatthias Ringwald }
27583e7cdd9SMatthias Ringwald 
276fc64f94aSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
277fc64f94aSMatthias Ringwald     int can_send = hci_can_send_acl_packet_now(con_handle);
2782125de09SMatthias Ringwald     if (!can_send){
2792125de09SMatthias Ringwald         int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
2802125de09SMatthias Ringwald         if (index >= 0){
2812125de09SMatthias Ringwald             fixed_channels[index].waiting_for_can_send_now = 1;
2822125de09SMatthias Ringwald         }
2832125de09SMatthias Ringwald     }
2842125de09SMatthias Ringwald     return can_send;
2853cab4fcaS[email protected] }
2863cab4fcaS[email protected] 
28796cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
28896cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
28996cbd662Smatthias.ringwald     if (channel) {
29096cbd662Smatthias.ringwald         return channel->remote_mtu;
29196cbd662Smatthias.ringwald     }
29296cbd662Smatthias.ringwald     return 0;
29396cbd662Smatthias.ringwald }
29496cbd662Smatthias.ringwald 
295ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
296665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
297665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
298665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
299665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3005932bd7cS[email protected]         if ( &channel->rtx == ts) {
3015932bd7cS[email protected]             return channel;
3025932bd7cS[email protected]         }
3035932bd7cS[email protected]     }
3045932bd7cS[email protected]     return NULL;
3055932bd7cS[email protected] }
3065932bd7cS[email protected] 
307ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3085932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
3095932bd7cS[email protected]     if (!ts) return;
3105932bd7cS[email protected] 
3115932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3125932bd7cS[email protected] 
3135932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3145932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3155932bd7cS[email protected]     // notify client
3165932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3175932bd7cS[email protected] 
3185932bd7cS[email protected]     // discard channel
3199dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
320665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3215932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3225932bd7cS[email protected] }
3235932bd7cS[email protected] 
3245932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3255932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
326528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3275932bd7cS[email protected] }
3285932bd7cS[email protected] 
3295932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3305932bd7cS[email protected]     l2cap_stop_rtx(channel);
331cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
332528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
333528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
334528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3355932bd7cS[email protected] }
3365932bd7cS[email protected] 
3375932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3385932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3395932bd7cS[email protected]     l2cap_stop_rtx(channel);
340528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
341528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
342528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3435932bd7cS[email protected] }
3445932bd7cS[email protected] 
34571de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
346ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
347ac301f95S[email protected] }
348ac301f95S[email protected] 
349df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
350ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
351df3354fcS[email protected] }
3525932bd7cS[email protected] 
3537f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
354b1d43497Smatthias.ringwald 
355a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3569da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
357b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
358b1d43497Smatthias.ringwald     }
359b1d43497Smatthias.ringwald 
3609da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3612a373862S[email protected]     hci_reserve_packet_buffer();
362facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
36358de5610Smatthias.ringwald     va_list argptr;
36458de5610Smatthias.ringwald     va_start(argptr, identifier);
36570efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
36658de5610Smatthias.ringwald     va_end(argptr);
3679da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
368826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
36958de5610Smatthias.ringwald }
37058de5610Smatthias.ringwald 
371a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3727f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
37370efece1S[email protected] 
374a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3759da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
37670efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
37770efece1S[email protected]     }
37870efece1S[email protected] 
3799da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3802a373862S[email protected]     hci_reserve_packet_buffer();
381facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
38270efece1S[email protected]     va_list argptr;
38370efece1S[email protected]     va_start(argptr, identifier);
38470efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
38570efece1S[email protected]     va_end(argptr);
3869da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
387826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
38870efece1S[email protected] }
38970efece1S[email protected] #endif
39070efece1S[email protected] 
391b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
392facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
393b1d43497Smatthias.ringwald }
3946218e6f1Smatthias.ringwald 
3956b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3966b4af23dS[email protected]     return hci_reserve_packet_buffer();
3976b4af23dS[email protected] }
3986b4af23dS[email protected] 
39968a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
40068a0fcf7S[email protected]     hci_release_packet_buffer();
40168a0fcf7S[email protected] }
40268a0fcf7S[email protected] 
40368a0fcf7S[email protected] 
404b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
405b1d43497Smatthias.ringwald 
406c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
407c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
408c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
409c8b9416aS[email protected]     }
410c8b9416aS[email protected] 
41158de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
412b1d43497Smatthias.ringwald     if (!channel) {
4139da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
414b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4156218e6f1Smatthias.ringwald     }
4166218e6f1Smatthias.ringwald 
417fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4189da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
419a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
420a35252c8S[email protected]     }
421a35252c8S[email protected] 
422fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
423b1d43497Smatthias.ringwald 
424facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
425b1d43497Smatthias.ringwald 
426e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
427e9772277S[email protected] 
428e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
429fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14));
43058de5610Smatthias.ringwald     // 2 - ACL length
431f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
43258de5610Smatthias.ringwald     // 4 - L2CAP packet length
433f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
43458de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
435f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
43658de5610Smatthias.ringwald     // send
437826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
43891b99603Smatthias.ringwald 
4396218e6f1Smatthias.ringwald     return err;
44058de5610Smatthias.ringwald }
44158de5610Smatthias.ringwald 
442fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
4432149f12eSmatthias.ringwald 
444c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
445c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4462149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4472149f12eSmatthias.ringwald     }
4482149f12eSmatthias.ringwald 
449fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
450fc64f94aSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
451c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
452c8b9416aS[email protected]     }
453c8b9416aS[email protected] 
454fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
4552149f12eSmatthias.ringwald 
456facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4572149f12eSmatthias.ringwald 
458e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
459e9772277S[email protected] 
460e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
461fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
4622149f12eSmatthias.ringwald     // 2 - ACL length
463f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4642149f12eSmatthias.ringwald     // 4 - L2CAP packet length
465f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4662149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
467f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4682149f12eSmatthias.ringwald     // send
469826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4702149f12eSmatthias.ringwald 
4712149f12eSmatthias.ringwald     return err;
4722149f12eSmatthias.ringwald }
4732149f12eSmatthias.ringwald 
474ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
475b1d43497Smatthias.ringwald 
476a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
477a35252c8S[email protected]     if (!channel) {
478ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
479a35252c8S[email protected]         return -1;   // TODO: define error
480a35252c8S[email protected]     }
481a35252c8S[email protected] 
482f0efaa57S[email protected]     if (len > channel->remote_mtu){
483ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
484f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
485f0efaa57S[email protected]     }
486f0efaa57S[email protected] 
487fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
488ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
489b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
490b1d43497Smatthias.ringwald     }
491b1d43497Smatthias.ringwald 
4922a373862S[email protected]     hci_reserve_packet_buffer();
493facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
494b1d43497Smatthias.ringwald 
495b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
496b1d43497Smatthias.ringwald 
497b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
498b1d43497Smatthias.ringwald }
499b1d43497Smatthias.ringwald 
500fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
5012149f12eSmatthias.ringwald 
502fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
503ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
5042149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
5052149f12eSmatthias.ringwald     }
5062149f12eSmatthias.ringwald 
5072a373862S[email protected]     hci_reserve_packet_buffer();
508facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
5092149f12eSmatthias.ringwald 
5102149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
5112149f12eSmatthias.ringwald 
512fc64f94aSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
5132149f12eSmatthias.ringwald }
5142149f12eSmatthias.ringwald 
515fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
516fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5170e37e417S[email protected] }
5180e37e417S[email protected] 
51928ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
52028ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
52128ca2b46S[email protected] }
52228ca2b46S[email protected] 
52328ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
52428ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
52528ca2b46S[email protected] }
52628ca2b46S[email protected] 
52728ca2b46S[email protected] 
528b1d43497Smatthias.ringwald 
5298158c421Smatthias.ringwald // MARK: L2CAP_RUN
5302cd0be45Smatthias.ringwald // process outstanding signaling tasks
5317f02f414SMatthias Ringwald static void l2cap_run(void){
5322b83fb7dSmatthias.ringwald 
53322c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
53422c29ab4SMatthias Ringwald 
5352b83fb7dSmatthias.ringwald     // check pending signaling responses
5362b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5372b83fb7dSmatthias.ringwald 
5382b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
539a35252c8S[email protected] 
540a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
541a35252c8S[email protected] 
5422b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5432b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
54463a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
545f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5462b83fb7dSmatthias.ringwald 
547f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
548f53da564S[email protected]         signaling_responses_pending--;
549f53da564S[email protected]         int i;
550f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
551f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
552f53da564S[email protected]         }
553f53da564S[email protected] 
554f53da564S[email protected]         switch (response_code){
5552b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5562b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5572bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5584d816277S[email protected]                 if (result == 0x0003){
5592bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5604d816277S[email protected]                 }
5612b360848Smatthias.ringwald                 break;
5622b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5632b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5642b83fb7dSmatthias.ringwald                 break;
5652b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5663b0484b3S[email protected]                 switch (infoType){
5673b0484b3S[email protected]                     case 1: { // Connectionless MTU
5683b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5693b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5703b0484b3S[email protected]                         break;
5713b0484b3S[email protected]                     }
5723b0484b3S[email protected]                     case 2: { // Extended Features Supported
573462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
574462e630dS[email protected]                         uint32_t features = 0x280;
5753b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5763b0484b3S[email protected]                         break;
5773b0484b3S[email protected]                     }
5783b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5793b0484b3S[email protected]                         uint8_t map[8];
5803b0484b3S[email protected]                         memset(map, 0, 8);
581462e630dS[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02)
5823b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5833b0484b3S[email protected]                         break;
5843b0484b3S[email protected]                     }
5853b0484b3S[email protected]                     default:
5862b83fb7dSmatthias.ringwald                         // all other types are not supported
5872b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5883b0484b3S[email protected]                         break;
5892b83fb7dSmatthias.ringwald                 }
5902b83fb7dSmatthias.ringwald                 break;
59163a7246aSmatthias.ringwald             case COMMAND_REJECT:
5925ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
593a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
59470efece1S[email protected]             case COMMAND_REJECT_LE:
59570efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
59663a7246aSmatthias.ringwald                 break;
59770efece1S[email protected] #endif
5982b83fb7dSmatthias.ringwald             default:
5992b83fb7dSmatthias.ringwald                 // should not happen
6002b83fb7dSmatthias.ringwald                 break;
6012b83fb7dSmatthias.ringwald         }
6022b83fb7dSmatthias.ringwald     }
6032b83fb7dSmatthias.ringwald 
604ae280e73Smatthias.ringwald     uint8_t  config_options[4];
605665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
606665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
607665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
608baf94f06S[email protected] 
609665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
61022c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6112cd0be45Smatthias.ringwald         switch (channel->state){
6122cd0be45Smatthias.ringwald 
613df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
614ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
615fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
616a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
617ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
618fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
619ad671560S[email protected]                 }
620ad671560S[email protected]                 break;
621ad671560S[email protected] 
62202b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
623baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
62464472d52Smatthias.ringwald                 // send connection request - set state first
62564472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
62602b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6278f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
62802b22dc4Smatthias.ringwald                 break;
62902b22dc4Smatthias.ringwald 
630e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
631fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
63222c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
633fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
634e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6359dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
636665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
637d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
638e7ff783cSmatthias.ringwald                 break;
639e7ff783cSmatthias.ringwald 
640552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
641fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
642fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
64328ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
644fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
645552d92a1Smatthias.ringwald                 break;
646552d92a1Smatthias.ringwald 
6476fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
648fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6496fdcc387Smatthias.ringwald                 // success, start l2cap handshake
650b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6516fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
652fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6535932bd7cS[email protected]                 l2cap_start_rtx(channel);
6546fdcc387Smatthias.ringwald                 break;
6556fdcc387Smatthias.ringwald 
656fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
657fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
65873cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
65963a7246aSmatthias.ringwald                     uint16_t flags = 0;
66028ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
66163a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
66263a7246aSmatthias.ringwald                         flags = 1;
66363a7246aSmatthias.ringwald                     } else {
66428ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
66563a7246aSmatthias.ringwald                     }
66663a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
667fc64f94aSMatthias 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);
66863a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
66963a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
67063a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
671f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
672fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
67363a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
67463a7246aSmatthias.ringwald                     } else {
675fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
67663a7246aSmatthias.ringwald                     }
67763a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
678fa8473a4Smatthias.ringwald                 }
67973cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
68028ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
68128ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
682b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
683ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
684ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
685f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
686fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6875932bd7cS[email protected]                     l2cap_start_rtx(channel);
688fa8473a4Smatthias.ringwald                 }
689fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
690552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
691552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
692fa8473a4Smatthias.ringwald                 }
693552d92a1Smatthias.ringwald                 break;
694552d92a1Smatthias.ringwald 
695e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
696fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
69722c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
698fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6995932bd7cS[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 :)
700756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
701e7ff783cSmatthias.ringwald                 break;
702e7ff783cSmatthias.ringwald 
703e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
704fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
705b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7062cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
707fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7082cd0be45Smatthias.ringwald                 break;
7092cd0be45Smatthias.ringwald             default:
7102cd0be45Smatthias.ringwald                 break;
7112cd0be45Smatthias.ringwald         }
7122cd0be45Smatthias.ringwald     }
713da886c03S[email protected] 
714a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
715da886c03S[email protected]     // send l2cap con paramter update if necessary
716da886c03S[email protected]     hci_connections_get_iterator(&it);
717665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
718665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7195d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
720b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
721da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
722b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
723b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
724b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
725b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
726b68d7bc3SMatthias Ringwald                 break;
727da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
728b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
729b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
730da886c03S[email protected]                 break;
731da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
732b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
733b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
734da886c03S[email protected]                 break;
735da886c03S[email protected]             default:
736da886c03S[email protected]                 break;
737da886c03S[email protected]         }
738da886c03S[email protected]     }
7394d7157c3S[email protected] #endif
740da886c03S[email protected] 
74122c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
7422cd0be45Smatthias.ringwald }
7432cd0be45Smatthias.ringwald 
7444aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7454ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
746fa8c92f6Smatthias.ringwald }
747fa8c92f6Smatthias.ringwald 
74871de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
7494ff786cfS[email protected]     return l2cap_max_mtu();
750e5e1518dS[email protected] }
751e5e1518dS[email protected] 
752fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
7532df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7545533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7552df5dadcS[email protected]         // success, start l2cap handshake
756fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
7572df5dadcS[email protected]         // check remote SSP feature first
7582df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7592df5dadcS[email protected]     }
7602df5dadcS[email protected] }
7612df5dadcS[email protected] 
7622df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7632df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7642df5dadcS[email protected] 
7652df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
766ac301f95S[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));
767fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7682df5dadcS[email protected]         // request security level 2
7692df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
770fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
7712df5dadcS[email protected]         return;
7722df5dadcS[email protected]     }
7732df5dadcS[email protected]     // fine, go ahead
7742df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7752df5dadcS[email protected] }
7762df5dadcS[email protected] 
7779077cb15SMatthias Ringwald /**
7789077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
7799077cb15SMatthias Ringwald  * @param packet_handler
7809077cb15SMatthias Ringwald  * @param address
7819077cb15SMatthias Ringwald  * @param psm
7829077cb15SMatthias Ringwald  * @param mtu
7839077cb15SMatthias Ringwald  * @param local_cid
7849077cb15SMatthias Ringwald  */
7859077cb15SMatthias Ringwald uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid){
7869077cb15SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
7879077cb15SMatthias Ringwald 
7889077cb15SMatthias Ringwald     // alloc structure
789fc64f94aSMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
790fc64f94aSMatthias Ringwald     if (!channel) {
7919077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
7929077cb15SMatthias Ringwald     }
7939077cb15SMatthias Ringwald 
7949077cb15SMatthias Ringwald      // Init memory (make valgrind happy)
795fc64f94aSMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
7969077cb15SMatthias Ringwald     // limit local mtu to max acl packet length - l2cap header
7979077cb15SMatthias Ringwald     if (mtu > l2cap_max_mtu()) {
7989077cb15SMatthias Ringwald         mtu = l2cap_max_mtu();
7999077cb15SMatthias Ringwald     }
8009077cb15SMatthias Ringwald 
8019077cb15SMatthias Ringwald     // fill in
802fc64f94aSMatthias Ringwald     bd_addr_copy(channel->address, address);
803fc64f94aSMatthias Ringwald     channel->psm = psm;
804fc64f94aSMatthias Ringwald     channel->con_handle = 0;
805fc64f94aSMatthias Ringwald     channel->packet_handler = channel_packet_handler;
806fc64f94aSMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
807fc64f94aSMatthias Ringwald     channel->local_mtu = mtu;
808fc64f94aSMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
8099077cb15SMatthias Ringwald 
8109077cb15SMatthias Ringwald     // set initial state
811fc64f94aSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
812fc64f94aSMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
813fc64f94aSMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
814fc64f94aSMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
815fc64f94aSMatthias Ringwald     channel->required_security_level = LEVEL_0;
8169077cb15SMatthias Ringwald 
8179077cb15SMatthias Ringwald     // add to connections list
818fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
8199077cb15SMatthias Ringwald 
8209077cb15SMatthias Ringwald     // store local_cid
8219077cb15SMatthias Ringwald     if (out_local_cid){
822fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
8239077cb15SMatthias Ringwald     }
8249077cb15SMatthias Ringwald 
8259077cb15SMatthias Ringwald     // check if hci connection is already usable
8269077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
8279077cb15SMatthias Ringwald     if (conn){
828add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
829fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
8309077cb15SMatthias Ringwald         // check if remote supported fearures are already received
8319077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
832fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
8339077cb15SMatthias Ringwald         }
8349077cb15SMatthias Ringwald     }
8359077cb15SMatthias Ringwald 
8369077cb15SMatthias Ringwald     l2cap_run();
8379077cb15SMatthias Ringwald 
8389077cb15SMatthias Ringwald     return 0;
8399077cb15SMatthias Ringwald }
8409077cb15SMatthias Ringwald 
841ce8f182eSMatthias Ringwald void
842ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
843e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
844b35f641cSmatthias.ringwald     // find channel for local_cid
845b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
846f62db1e3Smatthias.ringwald     if (channel) {
847e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
848f62db1e3Smatthias.ringwald     }
8492cd0be45Smatthias.ringwald     // process
8502cd0be45Smatthias.ringwald     l2cap_run();
85143625864Smatthias.ringwald }
8521e6aba47Smatthias.ringwald 
853afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
854665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
855665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
856665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
857665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
858058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
859c22aecc9S[email protected]         // channel for this address found
860c22aecc9S[email protected]         switch (channel->state){
861c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
862c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
863afde0c52Smatthias.ringwald                 // failure, forward error code
864afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
865afde0c52Smatthias.ringwald                 // discard channel
8669dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
867665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
868d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
869c22aecc9S[email protected]                 break;
870c22aecc9S[email protected]             default:
871c22aecc9S[email protected]                 break;
872afde0c52Smatthias.ringwald         }
873afde0c52Smatthias.ringwald     }
874afde0c52Smatthias.ringwald }
875afde0c52Smatthias.ringwald 
876afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
877665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
878665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
879665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
880665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
881058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
8822df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
883afde0c52Smatthias.ringwald         }
884afde0c52Smatthias.ringwald     }
8856fdcc387Smatthias.ringwald     // process
8866fdcc387Smatthias.ringwald     l2cap_run();
887afde0c52Smatthias.ringwald }
888b448a0e7Smatthias.ringwald 
88933c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
89033c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
89133c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
89233c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
89333c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
89433c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
895fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
89633c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
89734e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
89833c40538SMatthias Ringwald     }
8992125de09SMatthias Ringwald 
9002125de09SMatthias Ringwald     int i;
9012125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
902773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
9032125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
90434e7e577SMatthias Ringwald         int can_send;
90534e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
90634e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
90734e7e577SMatthias Ringwald         } else {
90834e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
90934e7e577SMatthias Ringwald         }
91034e7e577SMatthias Ringwald         if (!can_send) continue;
91134e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
91234e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
9132125de09SMatthias Ringwald     }
91433c40538SMatthias Ringwald }
91533c40538SMatthias Ringwald 
916d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
917afde0c52Smatthias.ringwald 
918afde0c52Smatthias.ringwald     bd_addr_t address;
919afde0c52Smatthias.ringwald     hci_con_handle_t handle;
920665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
9212d00edd4Smatthias.ringwald     int hci_con_used;
922afde0c52Smatthias.ringwald 
923*0e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
924afde0c52Smatthias.ringwald 
925afde0c52Smatthias.ringwald         // handle connection complete events
926afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
927724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
928afde0c52Smatthias.ringwald             if (packet[2] == 0){
929f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
930afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
931afde0c52Smatthias.ringwald             } else {
932afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
933afde0c52Smatthias.ringwald             }
934afde0c52Smatthias.ringwald             break;
935afde0c52Smatthias.ringwald 
936afde0c52Smatthias.ringwald         // handle successful create connection cancel command
937afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
938073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
939afde0c52Smatthias.ringwald                 if (packet[5] == 0){
940724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
941afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
942afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
94303cfbabcSmatthias.ringwald                 }
9441e6aba47Smatthias.ringwald             }
94539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
94639d59809Smatthias.ringwald             break;
94739d59809Smatthias.ringwald 
94839d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
94939d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
950afde0c52Smatthias.ringwald             break;
95127a923d0Smatthias.ringwald 
9521e6aba47Smatthias.ringwald         // handle disconnection complete events
953afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
954c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
955f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
956665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
957665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
958665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
959fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
96015ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
9619dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
962665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
963d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
96427a923d0Smatthias.ringwald             }
965afde0c52Smatthias.ringwald             break;
966fcadd0caSmatthias.ringwald 
96733c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
96863fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
9696218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
97002b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
97133c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
9726218e6f1Smatthias.ringwald             break;
9736218e6f1Smatthias.ringwald 
974ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
975afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
976f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
977bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
97880ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
9792d00edd4Smatthias.ringwald             hci_con_used = 0;
980665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
981665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
982665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
983fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
9842d00edd4Smatthias.ringwald                 hci_con_used = 1;
985c22aecc9S[email protected]                 break;
986ee091cf1Smatthias.ringwald             }
9872d00edd4Smatthias.ringwald             if (hci_con_used) break;
988d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
9899edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
990afde0c52Smatthias.ringwald             break;
991ee091cf1Smatthias.ringwald 
992df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
993f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
994665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
995665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
996665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
997fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
9982df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
999df3354fcS[email protected]                 break;
1000df3354fcS[email protected]             }
1001c22aecc9S[email protected]             break;
1002df3354fcS[email protected] 
10035611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1004f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1005bd63148eS[email protected]             log_info("l2cap - security level update");
1006665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1007665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1008665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1009fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10105533f01eS[email protected] 
1011bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1012bd63148eS[email protected] 
1013e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
10145533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
10155533f01eS[email protected] 
1016df3354fcS[email protected]                 switch (channel->state){
1017df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
10185533f01eS[email protected]                         if (actual_level >= required_level){
1019f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1020f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
10211eb2563eS[email protected]                         } else {
1022775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
10231eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
10241eb2563eS[email protected]                         }
1025df3354fcS[email protected]                         break;
1026df3354fcS[email protected] 
1027df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
10285533f01eS[email protected]                         if (actual_level >= required_level){
1029df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1030df3354fcS[email protected]                         } else {
1031df3354fcS[email protected]                             // disconnnect, authentication not good enough
1032df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1033df3354fcS[email protected]                         }
1034df3354fcS[email protected]                         break;
1035df3354fcS[email protected] 
1036df3354fcS[email protected]                     default:
1037df3354fcS[email protected]                         break;
1038df3354fcS[email protected]                 }
1039f85a9399S[email protected]             }
1040f85a9399S[email protected]             break;
1041f85a9399S[email protected] 
1042afde0c52Smatthias.ringwald         default:
1043afde0c52Smatthias.ringwald             break;
1044afde0c52Smatthias.ringwald     }
1045afde0c52Smatthias.ringwald 
1046bd63148eS[email protected]     l2cap_run();
10471e6aba47Smatthias.ringwald }
10481e6aba47Smatthias.ringwald 
1049afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1050b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1051e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1052e7ff783cSmatthias.ringwald     l2cap_run();
105384836b65Smatthias.ringwald }
105484836b65Smatthias.ringwald 
10552b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
10564cf56b4aSmatthias.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."
10572b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
10582b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
10592b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
10602b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
10612b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
10622b360848Smatthias.ringwald         signaling_responses_pending++;
10632b360848Smatthias.ringwald         l2cap_run();
10642b360848Smatthias.ringwald     }
10652b360848Smatthias.ringwald }
10662b360848Smatthias.ringwald 
1067b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1068645658c9Smatthias.ringwald 
10699da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1070645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1071645658c9Smatthias.ringwald     if (!service) {
1072645658c9Smatthias.ringwald         // 0x0002 PSM not supported
10732b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1074645658c9Smatthias.ringwald         return;
1075645658c9Smatthias.ringwald     }
1076645658c9Smatthias.ringwald 
10775061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1078645658c9Smatthias.ringwald     if (!hci_connection) {
10792b360848Smatthias.ringwald         //
10809da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1081645658c9Smatthias.ringwald         return;
1082645658c9Smatthias.ringwald     }
10832bd8b7e7S[email protected] 
1084645658c9Smatthias.ringwald     // alloc structure
10859da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1086bb69aaaeS[email protected]     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
10872b360848Smatthias.ringwald     if (!channel){
10882b360848Smatthias.ringwald         // 0x0004 No resources available
10892b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10902b360848Smatthias.ringwald         return;
10912b360848Smatthias.ringwald     }
1092c523d53dS[email protected]     // Init memory (make valgrind happy)
1093c523d53dS[email protected]     memset(channel, 0, sizeof(l2cap_channel_t));
1094645658c9Smatthias.ringwald     // fill in
1095058e3d6bSMatthias Ringwald     bd_addr_copy(channel->address, hci_connection->address);
1096169f8b28Smatthias.ringwald     channel->psm = psm;
1097fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1098f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1099b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1100b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1101fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
11020a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1103b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1104df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1105645658c9Smatthias.ringwald 
1106f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
11072985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
11082985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
11099775e25bSmatthias.ringwald     }
11109775e25bSmatthias.ringwald 
1111645658c9Smatthias.ringwald     // set initial state
1112df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1113ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1114e405ae81Smatthias.ringwald 
1115645658c9Smatthias.ringwald     // add to connections list
1116665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1117645658c9Smatthias.ringwald 
1118f85a9399S[email protected]     // assert security requirements
11191eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1120e405ae81Smatthias.ringwald }
1121645658c9Smatthias.ringwald 
1122ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1123e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1124b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1125e405ae81Smatthias.ringwald     if (!channel) {
1126ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1127e405ae81Smatthias.ringwald         return;
1128e405ae81Smatthias.ringwald     }
1129e405ae81Smatthias.ringwald 
1130552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1131e405ae81Smatthias.ringwald 
1132552d92a1Smatthias.ringwald     // process
1133552d92a1Smatthias.ringwald     l2cap_run();
1134e405ae81Smatthias.ringwald }
1135645658c9Smatthias.ringwald 
1136ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){
1137e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1138b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1139e405ae81Smatthias.ringwald     if (!channel) {
1140ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1141e405ae81Smatthias.ringwald         return;
1142e405ae81Smatthias.ringwald     }
1143e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1144e7ff783cSmatthias.ringwald     channel->reason = reason;
1145e7ff783cSmatthias.ringwald     l2cap_run();
1146645658c9Smatthias.ringwald }
1147645658c9Smatthias.ringwald 
11487f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1149b1988dceSmatthias.ringwald 
1150b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1151b1988dceSmatthias.ringwald 
1152f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
115363a7246aSmatthias.ringwald     if (flags & 1) {
115463a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
115563a7246aSmatthias.ringwald     }
115663a7246aSmatthias.ringwald 
11572784b77dSmatthias.ringwald     // accept the other's configuration options
1158f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
11593de7c0caSmatthias.ringwald     uint16_t pos     = 8;
11603de7c0caSmatthias.ringwald     while (pos < end_pos){
116163a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
116263a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
116363a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
116463a7246aSmatthias.ringwald         pos++;
11651dc511deSmatthias.ringwald         uint8_t length = command[pos++];
11661dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
116763a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1168f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
11699da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
117063a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
117163a7246aSmatthias.ringwald         }
11720fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11730fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1174f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
11750fe7a9d0S[email protected]         }
117663a7246aSmatthias.ringwald         // check for unknown options
117763a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1178c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
117963a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11801dc511deSmatthias.ringwald         }
11811dc511deSmatthias.ringwald         pos += length;
11821dc511deSmatthias.ringwald     }
11832784b77dSmatthias.ringwald }
11842784b77dSmatthias.ringwald 
1185fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11869da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
118773cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
118873cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1189019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1190019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1191fa8473a4Smatthias.ringwald     return 1;
1192fa8473a4Smatthias.ringwald }
1193fa8473a4Smatthias.ringwald 
1194fa8473a4Smatthias.ringwald 
11957f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11961e6aba47Smatthias.ringwald 
119700d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
119800d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
119938e5900eSmatthias.ringwald     uint16_t result = 0;
12001e6aba47Smatthias.ringwald 
12019da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1202b35f641cSmatthias.ringwald 
12039a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
12049a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
12059a011532Smatthias.ringwald         switch (channel->state){
1206fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
12079a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
12082b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
12099a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
12109a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
12119a011532Smatthias.ringwald                 break;
12129a011532Smatthias.ringwald 
12139a011532Smatthias.ringwald             default:
12149a011532Smatthias.ringwald                 // ignore in other states
12159a011532Smatthias.ringwald                 break;
12169a011532Smatthias.ringwald         }
12179a011532Smatthias.ringwald         return;
12189a011532Smatthias.ringwald     }
12199a011532Smatthias.ringwald 
122056081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
12211e6aba47Smatthias.ringwald     switch (channel->state) {
12221e6aba47Smatthias.ringwald 
12231e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
12241e6aba47Smatthias.ringwald             switch (code){
12251e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
12265932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1227f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
122838e5900eSmatthias.ringwald                     switch (result) {
122938e5900eSmatthias.ringwald                         case 0:
1230169f8b28Smatthias.ringwald                             // successful connection
1231f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1232fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
123328ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
123438e5900eSmatthias.ringwald                             break;
123538e5900eSmatthias.ringwald                         case 1:
12365932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
12375932bd7cS[email protected]                             l2cap_start_ertx(channel);
123838e5900eSmatthias.ringwald                             break;
123938e5900eSmatthias.ringwald                         default:
1240eb920dbeSmatthias.ringwald                             // channel closed
1241eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1242f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
124338e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1244eb920dbeSmatthias.ringwald 
1245eb920dbeSmatthias.ringwald                             // drop link key if security block
1246eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
124715a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1248eb920dbeSmatthias.ringwald                             }
1249eb920dbeSmatthias.ringwald 
1250eb920dbeSmatthias.ringwald                             // discard channel
1251665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1252d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
125338e5900eSmatthias.ringwald                             break;
12541e6aba47Smatthias.ringwald                     }
12551e6aba47Smatthias.ringwald                     break;
125638e5900eSmatthias.ringwald 
125738e5900eSmatthias.ringwald                 default:
12581e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
125938e5900eSmatthias.ringwald                     break;
12601e6aba47Smatthias.ringwald             }
12611e6aba47Smatthias.ringwald             break;
12621e6aba47Smatthias.ringwald 
1263fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1264f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1265ae280e73Smatthias.ringwald             switch (code) {
1266ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
126728ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1268ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
126963a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
127063a7246aSmatthias.ringwald                         // only done if continuation not set
127163a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
127263a7246aSmatthias.ringwald                     }
1273ae280e73Smatthias.ringwald                     break;
12741e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12755932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12765932bd7cS[email protected]                     switch (result){
12775932bd7cS[email protected]                         case 0: // success
12785932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12795932bd7cS[email protected]                             break;
12805932bd7cS[email protected]                         case 4: // pending
12815932bd7cS[email protected]                             l2cap_start_ertx(channel);
12825932bd7cS[email protected]                             break;
12835932bd7cS[email protected]                         default:
1284fe9d8984S[email protected]                             // retry on negative result
1285fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1286fe9d8984S[email protected]                             break;
1287fe9d8984S[email protected]                     }
12885a67bd4aSmatthias.ringwald                     break;
12895a67bd4aSmatthias.ringwald                 default:
12905a67bd4aSmatthias.ringwald                     break;
12911e6aba47Smatthias.ringwald             }
1292fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1293fa8473a4Smatthias.ringwald                 // for open:
12945a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1295fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1296c8e4258aSmatthias.ringwald             }
1297c8e4258aSmatthias.ringwald             break;
1298f62db1e3Smatthias.ringwald 
1299f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1300f62db1e3Smatthias.ringwald             switch (code) {
1301f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
130227a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
130327a923d0Smatthias.ringwald                     break;
13045a67bd4aSmatthias.ringwald                 default:
13055a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
13065a67bd4aSmatthias.ringwald                     break;
130727a923d0Smatthias.ringwald             }
130827a923d0Smatthias.ringwald             break;
130984836b65Smatthias.ringwald 
131084836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
131184836b65Smatthias.ringwald             // @TODO handle incoming requests
131284836b65Smatthias.ringwald             break;
131384836b65Smatthias.ringwald 
131484836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
131584836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
131684836b65Smatthias.ringwald             break;
131710642e45Smatthias.ringwald         default:
131810642e45Smatthias.ringwald             break;
131927a923d0Smatthias.ringwald     }
13209da54300S[email protected]     // log_info("new state %u", channel->state);
132127a923d0Smatthias.ringwald }
132227a923d0Smatthias.ringwald 
132300d93d79Smatthias.ringwald 
13247f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
132500d93d79Smatthias.ringwald 
132600d93d79Smatthias.ringwald     // get code, signalind identifier and command len
132700d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
132800d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
132900d93d79Smatthias.ringwald 
133000d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
133100d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
133263a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
133300d93d79Smatthias.ringwald         return;
133400d93d79Smatthias.ringwald     }
133500d93d79Smatthias.ringwald 
133600d93d79Smatthias.ringwald     // general commands without an assigned channel
133700d93d79Smatthias.ringwald     switch(code) {
133800d93d79Smatthias.ringwald 
133900d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1340f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1341f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
134200d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
13432b83fb7dSmatthias.ringwald             return;
134400d93d79Smatthias.ringwald         }
134500d93d79Smatthias.ringwald 
13462b360848Smatthias.ringwald         case ECHO_REQUEST:
13472b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
13482b83fb7dSmatthias.ringwald             return;
134900d93d79Smatthias.ringwald 
135000d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1351f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
13522b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
13532b83fb7dSmatthias.ringwald             return;
135400d93d79Smatthias.ringwald         }
135500d93d79Smatthias.ringwald 
135600d93d79Smatthias.ringwald         default:
135700d93d79Smatthias.ringwald             break;
135800d93d79Smatthias.ringwald     }
135900d93d79Smatthias.ringwald 
136000d93d79Smatthias.ringwald 
136100d93d79Smatthias.ringwald     // Get potential destination CID
1362f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
136300d93d79Smatthias.ringwald 
136400d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1365665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1366665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1367665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1368665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1369fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
137000d93d79Smatthias.ringwald         if (code & 1) {
1371b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1372b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
137300d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13744e32727eSmatthias.ringwald                 break;
137500d93d79Smatthias.ringwald             }
137600d93d79Smatthias.ringwald         } else {
1377b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
137800d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
137900d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13804e32727eSmatthias.ringwald                 break;
138100d93d79Smatthias.ringwald             }
138200d93d79Smatthias.ringwald         }
138300d93d79Smatthias.ringwald     }
138400d93d79Smatthias.ringwald }
138500d93d79Smatthias.ringwald 
1386d9a7306aSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ){
138700d93d79Smatthias.ringwald 
138800d93d79Smatthias.ringwald     // Get Channel ID
138900d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
139000d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
139100d93d79Smatthias.ringwald 
13925652b5ffS[email protected]     switch (channel_id) {
13935652b5ffS[email protected] 
13945652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13955652b5ffS[email protected] 
139600d93d79Smatthias.ringwald             uint16_t command_offset = 8;
139700d93d79Smatthias.ringwald             while (command_offset < size) {
139800d93d79Smatthias.ringwald 
139900d93d79Smatthias.ringwald                 // handle signaling commands
140000d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
140100d93d79Smatthias.ringwald 
140200d93d79Smatthias.ringwald                 // increment command_offset
1403f8fbdce0SMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
140400d93d79Smatthias.ringwald             }
14055652b5ffS[email protected]             break;
140600d93d79Smatthias.ringwald         }
140700d93d79Smatthias.ringwald 
14085652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
14095628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
14105628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
14115652b5ffS[email protected]             }
14125652b5ffS[email protected]             break;
14135652b5ffS[email protected] 
14145652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
14155628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
14165628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
14175652b5ffS[email protected]             }
14185652b5ffS[email protected]             break;
14195652b5ffS[email protected] 
1420462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
14215628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
14225628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1423462e630dS[email protected]             }
1424462e630dS[email protected]             break;
1425462e630dS[email protected] 
14261bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1427ccf076adS[email protected]             switch (packet[8]){
1428ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1429f8fbdce0SMatthias Ringwald                     uint16_t result = little_endian_read_16(packet, 12);
1430ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1431ccf076adS[email protected]                     break;
1432ccf076adS[email protected]                 }
1433ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1434ccf076adS[email protected]                     uint8_t event[10];
1435ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1436ccf076adS[email protected]                     event[1] = 8;
1437ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1438da886c03S[email protected] 
1439da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1440da886c03S[email protected]                     if (connection){
14416d91fb6cSMatthias Ringwald                         if (connection->role != HCI_ROLE_MASTER){
14426d91fb6cSMatthias Ringwald                             // reject command without notifying upper layer when not in master role
14436d91fb6cSMatthias Ringwald                             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14446d91fb6cSMatthias Ringwald                             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14456d91fb6cSMatthias Ringwald                             break;
14466d91fb6cSMatthias Ringwald                         }
1447da886c03S[email protected]                         int update_parameter = 1;
1448a4c06b28SMatthias Ringwald                         le_connection_parameter_range_t existing_range;
1449d8e8f12aSMatthias Ringwald                         gap_get_connection_parameter_range(existing_range);
1450f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_min = little_endian_read_16(packet,12);
1451f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_max = little_endian_read_16(packet,14);
1452f8fbdce0SMatthias Ringwald                         uint16_t le_conn_latency = little_endian_read_16(packet,16);
1453f8fbdce0SMatthias Ringwald                         uint16_t le_supervision_timeout = little_endian_read_16(packet,18);
1454da886c03S[email protected] 
1455da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1456da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1457da886c03S[email protected] 
1458da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1459da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1460da886c03S[email protected] 
1461da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1462da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1463da886c03S[email protected] 
1464da886c03S[email protected]                         if (update_parameter){
1465da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1466da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1467da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1468da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1469da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1470da886c03S[email protected]                         } else {
1471da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1472da886c03S[email protected]                         }
147379f53f1dSMatthias Ringwald                         connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1];
1474da886c03S[email protected]                     }
1475da886c03S[email protected] 
1476ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
147733c40538SMatthias Ringwald                     if (!l2cap_event_packet_handler) break;
147833c40538SMatthias Ringwald                     (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1479ccf076adS[email protected]                     break;
1480ccf076adS[email protected]                 }
1481ccf076adS[email protected]                 default: {
14821bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14831bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14841bbc0b23S[email protected]                     break;
14851bbc0b23S[email protected]                 }
1486ccf076adS[email protected]             }
1487ccf076adS[email protected]             break;
1488ccf076adS[email protected]         }
14891bbc0b23S[email protected] 
14905652b5ffS[email protected]         default: {
149100d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
149200d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
149300d93d79Smatthias.ringwald             if (channel) {
149417a9b554SMatthias Ringwald                 l2cap_dispatch_to_channel(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
149500d93d79Smatthias.ringwald             }
14965652b5ffS[email protected]             break;
14975652b5ffS[email protected]         }
14985652b5ffS[email protected]     }
149900d93d79Smatthias.ringwald 
15001eb2563eS[email protected]     l2cap_run();
15012718e2e7Smatthias.ringwald }
150200d93d79Smatthias.ringwald 
150315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
150427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1505f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1506f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1507f62db1e3Smatthias.ringwald     // discard channel
15089dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1509665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1510d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1511c8e4258aSmatthias.ringwald }
15121e6aba47Smatthias.ringwald 
15138f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1514665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1515665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1516665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1517665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
15189d9bbc01Smatthias.ringwald         if ( service->psm == psm){
15199d9bbc01Smatthias.ringwald             return service;
15209d9bbc01Smatthias.ringwald         };
15219d9bbc01Smatthias.ringwald     }
15229d9bbc01Smatthias.ringwald     return NULL;
15239d9bbc01Smatthias.ringwald }
15249d9bbc01Smatthias.ringwald 
15257192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
15267192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
15277192e786SMatthias Ringwald }
15287192e786SMatthias Ringwald 
1529e0abb8e7S[email protected] 
1530be2053a6SMatthias 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){
1531be2053a6SMatthias Ringwald 
1532be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1533e0abb8e7S[email protected] 
15344bb582b6Smatthias.ringwald     // check for alread registered psm
15354bb582b6Smatthias.ringwald     // TODO: emit error event
15369d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1537277abc2cSmatthias.ringwald     if (service) {
1538be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1539be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1540277abc2cSmatthias.ringwald     }
15419d9bbc01Smatthias.ringwald 
15424bb582b6Smatthias.ringwald     // alloc structure
15434bb582b6Smatthias.ringwald     // TODO: emit error event
1544bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1545277abc2cSmatthias.ringwald     if (!service) {
1546be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1547be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1548277abc2cSmatthias.ringwald     }
15499d9bbc01Smatthias.ringwald 
15509d9bbc01Smatthias.ringwald     // fill in
15519d9bbc01Smatthias.ringwald     service->psm = psm;
15529d9bbc01Smatthias.ringwald     service->mtu = mtu;
155305ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1554df3354fcS[email protected]     service->required_security_level = security_level;
15559d9bbc01Smatthias.ringwald 
15569d9bbc01Smatthias.ringwald     // add to services list
1557665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1558c0e866bfSmatthias.ringwald 
1559c0e866bfSmatthias.ringwald     // enable page scan
156015a95bd5SMatthias Ringwald     gap_connectable_control(1);
15615842b6d9Smatthias.ringwald 
1562be2053a6SMatthias Ringwald     return 0;
15639d9bbc01Smatthias.ringwald }
15649d9bbc01Smatthias.ringwald 
156502f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){
1566e0abb8e7S[email protected] 
1567e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1568e0abb8e7S[email protected] 
15699d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1570037d6e48Smatthias.ringwald     if (!service) return;
1571665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1572d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1573c0e866bfSmatthias.ringwald 
1574c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1575665d90f2SMatthias Ringwald     if (!btstack_linked_list_empty(&l2cap_services)) return;
157615a95bd5SMatthias Ringwald     gap_connectable_control(0);
15779d9bbc01Smatthias.ringwald }
15789d9bbc01Smatthias.ringwald 
15795652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
158005ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
15815628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
15825628cf69SMatthias Ringwald     if (index < 0) return;
15835628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
15845652b5ffS[email protected] }
15855652b5ffS[email protected] 
1586a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
15877192e786SMatthias Ringwald 
15887f02f414SMatthias Ringwald 
15897f02f414SMatthias Ringwald #if 0
15907192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
15917192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
15927192e786SMatthias Ringwald }
15937192e786SMatthias Ringwald /**
15947192e786SMatthias Ringwald  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
15957192e786SMatthias Ringwald  * @param
15967192e786SMatthias Ringwald  */
1597ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm,
15987192e786SMatthias Ringwald     uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){
15997192e786SMatthias Ringwald 
16007192e786SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection);
16017192e786SMatthias Ringwald 
16027192e786SMatthias Ringwald     // check for alread registered psm
16037192e786SMatthias Ringwald     // TODO: emit error event
16047192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
16057192e786SMatthias Ringwald     if (service) {
16067192e786SMatthias Ringwald         log_error("l2cap_le_register_service_internal: PSM %u already registered", psm);
16077192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
16087192e786SMatthias Ringwald         return;
16097192e786SMatthias Ringwald     }
16107192e786SMatthias Ringwald 
16117192e786SMatthias Ringwald     // alloc structure
16127192e786SMatthias Ringwald     // TODO: emit error event
16137192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
16147192e786SMatthias Ringwald     if (!service) {
16157192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
16167192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
16177192e786SMatthias Ringwald         return;
16187192e786SMatthias Ringwald     }
16197192e786SMatthias Ringwald 
16207192e786SMatthias Ringwald     // fill in
16217192e786SMatthias Ringwald     service->psm = psm;
16227192e786SMatthias Ringwald     service->mtu = mtu;
16237192e786SMatthias Ringwald     service->mps = mps;
16247192e786SMatthias Ringwald     service->packet_handler = packet_handler;
16257192e786SMatthias Ringwald     service->required_security_level = security_level;
16267192e786SMatthias Ringwald 
16277192e786SMatthias Ringwald     // add to services list
1628665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
16297192e786SMatthias Ringwald 
16307192e786SMatthias Ringwald     // done
16317192e786SMatthias Ringwald     l2cap_emit_service_registered(connection, 0, psm);
16327192e786SMatthias Ringwald }
16337192e786SMatthias Ringwald 
1634ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) {
16357192e786SMatthias Ringwald 
16367192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
16377192e786SMatthias Ringwald 
16387192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
16397192e786SMatthias Ringwald     if (!service) return;
1640665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
16417192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
16427192e786SMatthias Ringwald }
16437192e786SMatthias Ringwald #endif
16447f02f414SMatthias Ringwald #endif
1645