xref: /btstack/src/l2cap.c (revision 2125de09632edbd687e6b09dd1566fe517285732)
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"
50d3a9df87Smatthias.ringwald #include "btstack_memory.h"
5143625864Smatthias.ringwald 
5243625864Smatthias.ringwald #include <stdarg.h>
5343625864Smatthias.ringwald #include <string.h>
5443625864Smatthias.ringwald 
5543625864Smatthias.ringwald #include <stdio.h>
5643625864Smatthias.ringwald 
574c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
584c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
594c744e21Smatthias.ringwald 
6039bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
61e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6239bda6d5Smatthias.ringwald 
6300d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6800d93d79Smatthias.ringwald 
695628cf69SMatthias Ringwald // internal table
705628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
745628cf69SMatthias Ringwald 
7533c40538SMatthias Ringwald // prototypes
7633c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
7733c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
7833c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
7933c40538SMatthias Ringwald static void l2cap_emit_can_send_now(l2cap_channel_t *channel);
8033c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
8133c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
8233c40538SMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8333c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
8433c40538SMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size );
8533c40538SMatthias Ringwald 
865628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
875628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
88*2125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
895628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
905628cf69SMatthias Ringwald 
915628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
925628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
935628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
945628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
9539bda6d5Smatthias.ringwald 
9639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
972b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
982b83fb7dSmatthias.ringwald static int signaling_responses_pending;
992b83fb7dSmatthias.ringwald 
10033c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
10133c40538SMatthias Ringwald 
102fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
103fb37a842SMatthias Ringwald 
10433c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1055628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
10639bda6d5Smatthias.ringwald 
1075628cf69SMatthias Ringwald // static btstack_packet_handler_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL];
1085628cf69SMatthias Ringwald // static btstack_packet_handler_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL];
1095628cf69SMatthias Ringwald // static btstack_packet_handler_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL];
1105628cf69SMatthias Ringwald 
1115628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1125628cf69SMatthias Ringwald     switch (channel_id){
1135628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1145628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1155628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1165628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1175628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1185628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1195628cf69SMatthias Ringwald         default:
1205628cf69SMatthias Ringwald             return -1;
1215628cf69SMatthias Ringwald         }
1225628cf69SMatthias Ringwald }
12339bda6d5Smatthias.ringwald 
12471de195eSMatthias Ringwald void l2cap_init(void){
1252b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
126808a48abSmatthias.ringwald 
127f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
128f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1297192e786SMatthias Ringwald     l2cap_le_services = NULL;
1307192e786SMatthias Ringwald     l2cap_le_channels = NULL;
131f5454fc6Smatthias.ringwald 
13233c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1335628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
134f5454fc6Smatthias.ringwald 
135ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
136ac301f95S[email protected] 
137fcadd0caSmatthias.ringwald     //
1382718e2e7Smatthias.ringwald     // register callback with HCI
139fcadd0caSmatthias.ringwald     //
140d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
141fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
142fb37a842SMatthias Ringwald 
143d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
144fb37a842SMatthias Ringwald 
145c0e866bfSmatthias.ringwald     hci_connectable_control(0); // no services yet
146fcadd0caSmatthias.ringwald }
147fcadd0caSmatthias.ringwald 
148ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
14933c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1501e6aba47Smatthias.ringwald }
1511e6aba47Smatthias.ringwald 
15217a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
15358de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
15458de5610Smatthias.ringwald }
15558de5610Smatthias.ringwald 
15658de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
157c9dc710bS[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",
158e0abb8e7S[email protected]              status, bd_addr_to_str(channel->address), channel->handle, channel->psm,
159c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
160c9dc710bS[email protected]     uint8_t event[23];
16158de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
16258de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
16358de5610Smatthias.ringwald     event[2] = status;
16458de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
165f8fbdce0SMatthias Ringwald     little_endian_store_16(event,  9, channel->handle);
166f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
167f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
168f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
169f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
170f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
171f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
17258de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17317a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
17433c40538SMatthias Ringwald 
17533c40538SMatthias Ringwald     // if channel opened successfully, also send can send now if possible
17633c40538SMatthias Ringwald     if (status) return;
17733c40538SMatthias Ringwald     if (hci_can_send_acl_packet_now(channel->handle)){
17833c40538SMatthias Ringwald         l2cap_emit_can_send_now(channel);
17933c40538SMatthias Ringwald     } else {
18033c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
18133c40538SMatthias Ringwald     }
18258de5610Smatthias.ringwald }
18358de5610Smatthias.ringwald 
18458de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
185e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
18658de5610Smatthias.ringwald     uint8_t event[4];
18758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
18858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
189f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
19058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19117a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
19258de5610Smatthias.ringwald }
19358de5610Smatthias.ringwald 
19458de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
195e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
196e0abb8e7S[email protected]              bd_addr_to_str(channel->address), channel->handle,  channel->psm, channel->local_cid, channel->remote_cid);
19758de5610Smatthias.ringwald     uint8_t event[16];
19858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
19958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
20058de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
201f8fbdce0SMatthias Ringwald     little_endian_store_16(event,  8, channel->handle);
202f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
203f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
204f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
20558de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20617a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2070af41d30Smatthias.ringwald }
208808a48abSmatthias.ringwald 
20933c40538SMatthias Ringwald static void l2cap_emit_can_send_now(l2cap_channel_t *channel) {
21033c40538SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel->local_cid);
21133c40538SMatthias Ringwald     uint8_t event[4];
21233c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
21333c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
21433c40538SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
21533c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21633c40538SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
21733c40538SMatthias Ringwald }
21833c40538SMatthias Ringwald 
2197f02f414SMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){
220ccf076adS[email protected]     uint8_t event[6];
221ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
222ccf076adS[email protected]     event[1] = 4;
223f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, handle);
224f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
225ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22633c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
22733c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
228ccf076adS[email protected] }
229ccf076adS[email protected] 
2307f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
231665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
232665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
233665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
234665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
235b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
236f62db1e3Smatthias.ringwald             return channel;
237f62db1e3Smatthias.ringwald         }
238f62db1e3Smatthias.ringwald     }
239f62db1e3Smatthias.ringwald     return NULL;
240f62db1e3Smatthias.ringwald }
241f62db1e3Smatthias.ringwald 
2426b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2436b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2446b1fde37Smatthias.ringwald     if (!channel) return 0;
24533c40538SMatthias Ringwald     int can_send = hci_can_send_acl_packet_now(channel->handle);
24633c40538SMatthias Ringwald     if (!can_send){
24733c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
24833c40538SMatthias Ringwald     }
24933c40538SMatthias Ringwald     return can_send;
2507856fb31S[email protected] }
2517856fb31S[email protected] 
25283e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
25383e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
25483e7cdd9SMatthias Ringwald     if (!channel) return 0;
25533c40538SMatthias Ringwald     int can_send = hci_can_send_prepared_acl_packet_now(channel->handle);
25633c40538SMatthias Ringwald     if (!can_send){
25733c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
25833c40538SMatthias Ringwald     }
25933c40538SMatthias Ringwald     return can_send;
26083e7cdd9SMatthias Ringwald }
26183e7cdd9SMatthias Ringwald 
262adbe29e8SMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
263*2125de09SMatthias Ringwald     int can_send = hci_can_send_acl_packet_now(handle);
264*2125de09SMatthias Ringwald     if (!can_send){
265*2125de09SMatthias Ringwald         int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
266*2125de09SMatthias Ringwald         if (index >= 0){
267*2125de09SMatthias Ringwald             fixed_channels[index].waiting_for_can_send_now = 1;
268*2125de09SMatthias Ringwald         }
269*2125de09SMatthias Ringwald     }
270*2125de09SMatthias Ringwald     return can_send;
2713cab4fcaS[email protected] }
2723cab4fcaS[email protected] 
27396cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
27496cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
27596cbd662Smatthias.ringwald     if (channel) {
27696cbd662Smatthias.ringwald         return channel->remote_mtu;
27796cbd662Smatthias.ringwald     }
27896cbd662Smatthias.ringwald     return 0;
27996cbd662Smatthias.ringwald }
28096cbd662Smatthias.ringwald 
281ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
282665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
283665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
284665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
285665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2865932bd7cS[email protected]         if ( &channel->rtx == ts) {
2875932bd7cS[email protected]             return channel;
2885932bd7cS[email protected]         }
2895932bd7cS[email protected]     }
2905932bd7cS[email protected]     return NULL;
2915932bd7cS[email protected] }
2925932bd7cS[email protected] 
293ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
2945932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2955932bd7cS[email protected]     if (!ts) return;
2965932bd7cS[email protected] 
2975932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2985932bd7cS[email protected] 
2995932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3005932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3015932bd7cS[email protected]     // notify client
3025932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3035932bd7cS[email protected] 
3045932bd7cS[email protected]     // discard channel
3059dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
306665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3075932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3085932bd7cS[email protected] }
3095932bd7cS[email protected] 
3105932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3115932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
312528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3135932bd7cS[email protected] }
3145932bd7cS[email protected] 
3155932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3165932bd7cS[email protected]     l2cap_stop_rtx(channel);
317cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
318528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
319528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
320528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3215932bd7cS[email protected] }
3225932bd7cS[email protected] 
3235932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3245932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3255932bd7cS[email protected]     l2cap_stop_rtx(channel);
326528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
327528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
328528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3295932bd7cS[email protected] }
3305932bd7cS[email protected] 
33171de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
332ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
333ac301f95S[email protected] }
334ac301f95S[email protected] 
335df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
336ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
337df3354fcS[email protected] }
3385932bd7cS[email protected] 
3397f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
340b1d43497Smatthias.ringwald 
341a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3429da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
343b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
344b1d43497Smatthias.ringwald     }
345b1d43497Smatthias.ringwald 
3469da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3472a373862S[email protected]     hci_reserve_packet_buffer();
348facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
34958de5610Smatthias.ringwald     va_list argptr;
35058de5610Smatthias.ringwald     va_start(argptr, identifier);
35170efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
35258de5610Smatthias.ringwald     va_end(argptr);
3539da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
354826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
35558de5610Smatthias.ringwald }
35658de5610Smatthias.ringwald 
357a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3587f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
35970efece1S[email protected] 
360a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3619da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
36270efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
36370efece1S[email protected]     }
36470efece1S[email protected] 
3659da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3662a373862S[email protected]     hci_reserve_packet_buffer();
367facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
36870efece1S[email protected]     va_list argptr;
36970efece1S[email protected]     va_start(argptr, identifier);
37070efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
37170efece1S[email protected]     va_end(argptr);
3729da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
373826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
37470efece1S[email protected] }
37570efece1S[email protected] #endif
37670efece1S[email protected] 
377b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
378facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
379b1d43497Smatthias.ringwald }
3806218e6f1Smatthias.ringwald 
3816b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3826b4af23dS[email protected]     return hci_reserve_packet_buffer();
3836b4af23dS[email protected] }
3846b4af23dS[email protected] 
38568a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
38668a0fcf7S[email protected]     hci_release_packet_buffer();
38768a0fcf7S[email protected] }
38868a0fcf7S[email protected] 
38968a0fcf7S[email protected] 
390b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
391b1d43497Smatthias.ringwald 
392c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
393c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
394c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
395c8b9416aS[email protected]     }
396c8b9416aS[email protected] 
39758de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
398b1d43497Smatthias.ringwald     if (!channel) {
3999da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
400b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4016218e6f1Smatthias.ringwald     }
4026218e6f1Smatthias.ringwald 
403a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(channel->handle)){
4049da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
405a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
406a35252c8S[email protected]     }
407a35252c8S[email protected] 
4083aff022fSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle);
409b1d43497Smatthias.ringwald 
410facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
411b1d43497Smatthias.ringwald 
412e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
413e9772277S[email protected] 
414e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
415f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14));
41658de5610Smatthias.ringwald     // 2 - ACL length
417f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
41858de5610Smatthias.ringwald     // 4 - L2CAP packet length
419f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
42058de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
421f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
42258de5610Smatthias.ringwald     // send
423826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
42491b99603Smatthias.ringwald 
4256218e6f1Smatthias.ringwald     return err;
42658de5610Smatthias.ringwald }
42758de5610Smatthias.ringwald 
4282149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
4292149f12eSmatthias.ringwald 
430c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
431c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4322149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4332149f12eSmatthias.ringwald     }
4342149f12eSmatthias.ringwald 
435a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(handle)){
4369da54300S[email protected]         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid);
437c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
438c8b9416aS[email protected]     }
439c8b9416aS[email protected] 
4409da54300S[email protected]     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid);
4412149f12eSmatthias.ringwald 
442facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4432149f12eSmatthias.ringwald 
444e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
445e9772277S[email protected] 
446e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
447f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
4482149f12eSmatthias.ringwald     // 2 - ACL length
449f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4502149f12eSmatthias.ringwald     // 4 - L2CAP packet length
451f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4522149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
453f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4542149f12eSmatthias.ringwald     // send
455826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4562149f12eSmatthias.ringwald 
4572149f12eSmatthias.ringwald     return err;
4582149f12eSmatthias.ringwald }
4592149f12eSmatthias.ringwald 
460ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
461b1d43497Smatthias.ringwald 
462a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
463a35252c8S[email protected]     if (!channel) {
464ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
465a35252c8S[email protected]         return -1;   // TODO: define error
466a35252c8S[email protected]     }
467a35252c8S[email protected] 
468f0efaa57S[email protected]     if (len > channel->remote_mtu){
469ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
470f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
471f0efaa57S[email protected]     }
472f0efaa57S[email protected] 
473a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(channel->handle)){
474ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
475b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
476b1d43497Smatthias.ringwald     }
477b1d43497Smatthias.ringwald 
4782a373862S[email protected]     hci_reserve_packet_buffer();
479facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
480b1d43497Smatthias.ringwald 
481b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
482b1d43497Smatthias.ringwald 
483b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
484b1d43497Smatthias.ringwald }
485b1d43497Smatthias.ringwald 
4862149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
4872149f12eSmatthias.ringwald 
488a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
489ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
4902149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4912149f12eSmatthias.ringwald     }
4922149f12eSmatthias.ringwald 
4932a373862S[email protected]     hci_reserve_packet_buffer();
494facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4952149f12eSmatthias.ringwald 
4962149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4972149f12eSmatthias.ringwald 
4982149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4992149f12eSmatthias.ringwald }
5002149f12eSmatthias.ringwald 
5010e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){
5020e37e417S[email protected]     return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data);
5030e37e417S[email protected] }
5040e37e417S[email protected] 
50528ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
50628ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
50728ca2b46S[email protected] }
50828ca2b46S[email protected] 
50928ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
51028ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
51128ca2b46S[email protected] }
51228ca2b46S[email protected] 
51328ca2b46S[email protected] 
514b1d43497Smatthias.ringwald 
5158158c421Smatthias.ringwald // MARK: L2CAP_RUN
5162cd0be45Smatthias.ringwald // process outstanding signaling tasks
5177f02f414SMatthias Ringwald static void l2cap_run(void){
5182b83fb7dSmatthias.ringwald 
51922c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
52022c29ab4SMatthias Ringwald 
5212b83fb7dSmatthias.ringwald     // check pending signaling responses
5222b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5232b83fb7dSmatthias.ringwald 
5242b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
525a35252c8S[email protected] 
526a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
527a35252c8S[email protected] 
5282b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5292b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
53063a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
531f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5322b83fb7dSmatthias.ringwald 
533f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
534f53da564S[email protected]         signaling_responses_pending--;
535f53da564S[email protected]         int i;
536f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
537f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
538f53da564S[email protected]         }
539f53da564S[email protected] 
540f53da564S[email protected]         switch (response_code){
5412b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5422b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5432bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5444d816277S[email protected]                 if (result == 0x0003){
5452bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5464d816277S[email protected]                 }
5472b360848Smatthias.ringwald                 break;
5482b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5492b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5502b83fb7dSmatthias.ringwald                 break;
5512b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5523b0484b3S[email protected]                 switch (infoType){
5533b0484b3S[email protected]                     case 1: { // Connectionless MTU
5543b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5553b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5563b0484b3S[email protected]                         break;
5573b0484b3S[email protected]                     }
5583b0484b3S[email protected]                     case 2: { // Extended Features Supported
559462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
560462e630dS[email protected]                         uint32_t features = 0x280;
5613b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5623b0484b3S[email protected]                         break;
5633b0484b3S[email protected]                     }
5643b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5653b0484b3S[email protected]                         uint8_t map[8];
5663b0484b3S[email protected]                         memset(map, 0, 8);
567462e630dS[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02)
5683b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5693b0484b3S[email protected]                         break;
5703b0484b3S[email protected]                     }
5713b0484b3S[email protected]                     default:
5722b83fb7dSmatthias.ringwald                         // all other types are not supported
5732b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5743b0484b3S[email protected]                         break;
5752b83fb7dSmatthias.ringwald                 }
5762b83fb7dSmatthias.ringwald                 break;
57763a7246aSmatthias.ringwald             case COMMAND_REJECT:
5785ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
579a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
58070efece1S[email protected]             case COMMAND_REJECT_LE:
58170efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
58263a7246aSmatthias.ringwald                 break;
58370efece1S[email protected] #endif
5842b83fb7dSmatthias.ringwald             default:
5852b83fb7dSmatthias.ringwald                 // should not happen
5862b83fb7dSmatthias.ringwald                 break;
5872b83fb7dSmatthias.ringwald         }
5882b83fb7dSmatthias.ringwald     }
5892b83fb7dSmatthias.ringwald 
590ae280e73Smatthias.ringwald     uint8_t  config_options[4];
591665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
592665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
593665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
594baf94f06S[email protected] 
595665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
59622c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
5972cd0be45Smatthias.ringwald         switch (channel->state){
5982cd0be45Smatthias.ringwald 
599df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
600ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
601baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
602a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
603ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
604a00031e2S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
605ad671560S[email protected]                 }
606ad671560S[email protected]                 break;
607ad671560S[email protected] 
60802b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
609baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
61064472d52Smatthias.ringwald                 // send connection request - set state first
61164472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
61202b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6138f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
61402b22dc4Smatthias.ringwald                 break;
61502b22dc4Smatthias.ringwald 
616e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
617baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
61822c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
6191eb2563eS[email protected]                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
620e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6219dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
622665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
623d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
624e7ff783cSmatthias.ringwald                 break;
625e7ff783cSmatthias.ringwald 
626552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
627baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
628fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
62928ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
6302a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
631552d92a1Smatthias.ringwald                 break;
632552d92a1Smatthias.ringwald 
6336fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
634baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
6356fdcc387Smatthias.ringwald                 // success, start l2cap handshake
636b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6376fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
6382a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6395932bd7cS[email protected]                 l2cap_start_rtx(channel);
6406fdcc387Smatthias.ringwald                 break;
6416fdcc387Smatthias.ringwald 
642fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
643baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
64473cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
64563a7246aSmatthias.ringwald                     uint16_t flags = 0;
64628ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
64763a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
64863a7246aSmatthias.ringwald                         flags = 1;
64963a7246aSmatthias.ringwald                     } else {
65028ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
65163a7246aSmatthias.ringwald                     }
65263a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
65363a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
65463a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
65563a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
65663a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
657f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
65863a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
65963a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
66063a7246aSmatthias.ringwald                     } else {
66163a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
66263a7246aSmatthias.ringwald                     }
66363a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
664fa8473a4Smatthias.ringwald                 }
66573cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
66628ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
66728ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
668b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
669ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
670ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
671f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
672b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6735932bd7cS[email protected]                     l2cap_start_rtx(channel);
674fa8473a4Smatthias.ringwald                 }
675fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
676552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
677552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
678fa8473a4Smatthias.ringwald                 }
679552d92a1Smatthias.ringwald                 break;
680552d92a1Smatthias.ringwald 
681e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
682baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
68322c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
684b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6855932bd7cS[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 :)
686756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
687e7ff783cSmatthias.ringwald                 break;
688e7ff783cSmatthias.ringwald 
689e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
690baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
691b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6922cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
6932a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
6942cd0be45Smatthias.ringwald                 break;
6952cd0be45Smatthias.ringwald             default:
6962cd0be45Smatthias.ringwald                 break;
6972cd0be45Smatthias.ringwald         }
6982cd0be45Smatthias.ringwald     }
699da886c03S[email protected] 
700a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
701da886c03S[email protected]     // send l2cap con paramter update if necessary
702da886c03S[email protected]     hci_connections_get_iterator(&it);
703665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
704665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7055d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
706b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
707da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
708b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
709b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
710b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
711b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
712b68d7bc3SMatthias Ringwald                 break;
713da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
714b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
715b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
716da886c03S[email protected]                 break;
717da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
718b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
719b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
720da886c03S[email protected]                 break;
721da886c03S[email protected]             default:
722da886c03S[email protected]                 break;
723da886c03S[email protected]         }
724da886c03S[email protected]     }
7254d7157c3S[email protected] #endif
726da886c03S[email protected] 
72722c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
7282cd0be45Smatthias.ringwald }
7292cd0be45Smatthias.ringwald 
7304aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7314ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
732fa8c92f6Smatthias.ringwald }
733fa8c92f6Smatthias.ringwald 
73471de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
7354ff786cfS[email protected]     return l2cap_max_mtu();
736e5e1518dS[email protected] }
737e5e1518dS[email protected] 
7382df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){
7392df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7405533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7412df5dadcS[email protected]         // success, start l2cap handshake
7422df5dadcS[email protected]         channel->handle = handle;
7432df5dadcS[email protected]         // check remote SSP feature first
7442df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7452df5dadcS[email protected]     }
7462df5dadcS[email protected] }
7472df5dadcS[email protected] 
7482df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7492df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7502df5dadcS[email protected] 
7512df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
752ac301f95S[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));
7532df5dadcS[email protected]     if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7542df5dadcS[email protected]         // request security level 2
7552df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
7561429b2d6S[email protected]         gap_request_security_level(channel->handle, LEVEL_2);
7572df5dadcS[email protected]         return;
7582df5dadcS[email protected]     }
7592df5dadcS[email protected]     // fine, go ahead
7602df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7612df5dadcS[email protected] }
7622df5dadcS[email protected] 
7639077cb15SMatthias Ringwald /**
7649077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
7659077cb15SMatthias Ringwald  * @param packet_handler
7669077cb15SMatthias Ringwald  * @param address
7679077cb15SMatthias Ringwald  * @param psm
7689077cb15SMatthias Ringwald  * @param mtu
7699077cb15SMatthias Ringwald  * @param local_cid
7709077cb15SMatthias Ringwald  */
7719077cb15SMatthias 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){
7729077cb15SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
7739077cb15SMatthias Ringwald 
7749077cb15SMatthias Ringwald     // alloc structure
7759077cb15SMatthias Ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
7769077cb15SMatthias Ringwald     if (!chan) {
7779077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
7789077cb15SMatthias Ringwald     }
7799077cb15SMatthias Ringwald 
7809077cb15SMatthias Ringwald      // Init memory (make valgrind happy)
7819077cb15SMatthias Ringwald     memset(chan, 0, sizeof(l2cap_channel_t));
7829077cb15SMatthias Ringwald     // limit local mtu to max acl packet length - l2cap header
7839077cb15SMatthias Ringwald     if (mtu > l2cap_max_mtu()) {
7849077cb15SMatthias Ringwald         mtu = l2cap_max_mtu();
7859077cb15SMatthias Ringwald     }
7869077cb15SMatthias Ringwald 
7879077cb15SMatthias Ringwald     // fill in
7889077cb15SMatthias Ringwald     BD_ADDR_COPY(chan->address, address);
7899077cb15SMatthias Ringwald     chan->psm = psm;
7909077cb15SMatthias Ringwald     chan->handle = 0;
7919077cb15SMatthias Ringwald     chan->packet_handler = channel_packet_handler;
7929077cb15SMatthias Ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
7939077cb15SMatthias Ringwald     chan->local_mtu = mtu;
7949077cb15SMatthias Ringwald     chan->local_cid = l2cap_next_local_cid();
7959077cb15SMatthias Ringwald 
7969077cb15SMatthias Ringwald     // set initial state
7979077cb15SMatthias Ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
7989077cb15SMatthias Ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
7999077cb15SMatthias Ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
8009077cb15SMatthias Ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
8019077cb15SMatthias Ringwald     chan->required_security_level = LEVEL_0;
8029077cb15SMatthias Ringwald 
8039077cb15SMatthias Ringwald     // add to connections list
804665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan);
8059077cb15SMatthias Ringwald 
8069077cb15SMatthias Ringwald     // store local_cid
8079077cb15SMatthias Ringwald     if (out_local_cid){
8089077cb15SMatthias Ringwald        *out_local_cid = chan->local_cid;
8099077cb15SMatthias Ringwald     }
8109077cb15SMatthias Ringwald 
8119077cb15SMatthias Ringwald     // check if hci connection is already usable
8129077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
8139077cb15SMatthias Ringwald     if (conn){
814add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
8159077cb15SMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, chan);
8169077cb15SMatthias Ringwald         // check if remote supported fearures are already received
8179077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
8189077cb15SMatthias Ringwald             l2cap_handle_remote_supported_features_received(chan);
8199077cb15SMatthias Ringwald         }
8209077cb15SMatthias Ringwald     }
8219077cb15SMatthias Ringwald 
8229077cb15SMatthias Ringwald     l2cap_run();
8239077cb15SMatthias Ringwald 
8249077cb15SMatthias Ringwald     return 0;
8259077cb15SMatthias Ringwald }
8269077cb15SMatthias Ringwald 
827ce8f182eSMatthias Ringwald void
828ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
829e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
830b35f641cSmatthias.ringwald     // find channel for local_cid
831b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
832f62db1e3Smatthias.ringwald     if (channel) {
833e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
834f62db1e3Smatthias.ringwald     }
8352cd0be45Smatthias.ringwald     // process
8362cd0be45Smatthias.ringwald     l2cap_run();
83743625864Smatthias.ringwald }
8381e6aba47Smatthias.ringwald 
839afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
840665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
841665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
842665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
843665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
844c22aecc9S[email protected]         if ( BD_ADDR_CMP( channel->address, address) != 0) continue;
845c22aecc9S[email protected]         // channel for this address found
846c22aecc9S[email protected]         switch (channel->state){
847c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
848c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
849afde0c52Smatthias.ringwald                 // failure, forward error code
850afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
851afde0c52Smatthias.ringwald                 // discard channel
8529dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
853665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
854d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
855c22aecc9S[email protected]                 break;
856c22aecc9S[email protected]             default:
857c22aecc9S[email protected]                 break;
858afde0c52Smatthias.ringwald         }
859afde0c52Smatthias.ringwald     }
860afde0c52Smatthias.ringwald }
861afde0c52Smatthias.ringwald 
862afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
863665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
864665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
865665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
866665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
867afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
8682df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
869afde0c52Smatthias.ringwald         }
870afde0c52Smatthias.ringwald     }
8716fdcc387Smatthias.ringwald     // process
8726fdcc387Smatthias.ringwald     l2cap_run();
873afde0c52Smatthias.ringwald }
874b448a0e7Smatthias.ringwald 
87533c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
87633c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
87733c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
87833c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
87933c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
88033c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
88133c40538SMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->handle)) continue;
88233c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
88333c40538SMatthias Ringwald         l2cap_emit_can_send_now(channel);
88433c40538SMatthias Ringwald     }
885*2125de09SMatthias Ringwald 
886*2125de09SMatthias Ringwald     int i;
887*2125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
888*2125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
889*2125de09SMatthias Ringwald         // can we send now?
890*2125de09SMatthias Ringwald         //
891*2125de09SMatthias Ringwald 
892*2125de09SMatthias Ringwald     }
89333c40538SMatthias Ringwald }
89433c40538SMatthias Ringwald 
895d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
896afde0c52Smatthias.ringwald 
897afde0c52Smatthias.ringwald     bd_addr_t address;
898afde0c52Smatthias.ringwald     hci_con_handle_t handle;
899665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
9002d00edd4Smatthias.ringwald     int hci_con_used;
901afde0c52Smatthias.ringwald 
902afde0c52Smatthias.ringwald     switch(packet[0]){
903afde0c52Smatthias.ringwald 
904afde0c52Smatthias.ringwald         // handle connection complete events
905afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
906afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
907afde0c52Smatthias.ringwald             if (packet[2] == 0){
908f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
909afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
910afde0c52Smatthias.ringwald             } else {
911afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
912afde0c52Smatthias.ringwald             }
913afde0c52Smatthias.ringwald             break;
914afde0c52Smatthias.ringwald 
915afde0c52Smatthias.ringwald         // handle successful create connection cancel command
916afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
917afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
918afde0c52Smatthias.ringwald                 if (packet[5] == 0){
919afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
920afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
921afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
92203cfbabcSmatthias.ringwald                 }
9231e6aba47Smatthias.ringwald             }
92439d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
92539d59809Smatthias.ringwald             break;
92639d59809Smatthias.ringwald 
92739d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
92839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
929afde0c52Smatthias.ringwald             break;
93027a923d0Smatthias.ringwald 
9311e6aba47Smatthias.ringwald         // handle disconnection complete events
932afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
933c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
934f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
935665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
936665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
937665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
938c22aecc9S[email protected]                 if (channel->handle != handle) continue;
93915ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
9409dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
941665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
942d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
94327a923d0Smatthias.ringwald             }
944afde0c52Smatthias.ringwald             break;
945fcadd0caSmatthias.ringwald 
94633c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
94733c40538SMatthias Ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
9486218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
94902b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
95033c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
9516218e6f1Smatthias.ringwald             break;
9526218e6f1Smatthias.ringwald 
953ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
954afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
955f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
956bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
95780ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
9582d00edd4Smatthias.ringwald             hci_con_used = 0;
959665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
960665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
961665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
962c22aecc9S[email protected]                 if (channel->handle != handle) continue;
9632d00edd4Smatthias.ringwald                 hci_con_used = 1;
964c22aecc9S[email protected]                 break;
965ee091cf1Smatthias.ringwald             }
9662d00edd4Smatthias.ringwald             if (hci_con_used) break;
967d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
9689edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
969afde0c52Smatthias.ringwald             break;
970ee091cf1Smatthias.ringwald 
971df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
972f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
973665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
974665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
975665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
976df3354fcS[email protected]                 if (channel->handle != handle) continue;
9772df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
978df3354fcS[email protected]                 break;
979df3354fcS[email protected]             }
980c22aecc9S[email protected]             break;
981df3354fcS[email protected] 
9825611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
983f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
984bd63148eS[email protected]             log_info("l2cap - security level update");
985665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
986665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
987665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
988f85a9399S[email protected]                 if (channel->handle != handle) continue;
9895533f01eS[email protected] 
990bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
991bd63148eS[email protected] 
992e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
9935533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
9945533f01eS[email protected] 
995df3354fcS[email protected]                 switch (channel->state){
996df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
9975533f01eS[email protected]                         if (actual_level >= required_level){
998f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
999f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
10001eb2563eS[email protected]                         } else {
1001775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
10021eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
10031eb2563eS[email protected]                         }
1004df3354fcS[email protected]                         break;
1005df3354fcS[email protected] 
1006df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
10075533f01eS[email protected]                         if (actual_level >= required_level){
1008df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1009df3354fcS[email protected]                         } else {
1010df3354fcS[email protected]                             // disconnnect, authentication not good enough
1011df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1012df3354fcS[email protected]                         }
1013df3354fcS[email protected]                         break;
1014df3354fcS[email protected] 
1015df3354fcS[email protected]                     default:
1016df3354fcS[email protected]                         break;
1017df3354fcS[email protected]                 }
1018f85a9399S[email protected]             }
1019f85a9399S[email protected]             break;
1020f85a9399S[email protected] 
1021afde0c52Smatthias.ringwald         default:
1022afde0c52Smatthias.ringwald             break;
1023afde0c52Smatthias.ringwald     }
1024afde0c52Smatthias.ringwald 
1025bd63148eS[email protected]     l2cap_run();
10261e6aba47Smatthias.ringwald }
10271e6aba47Smatthias.ringwald 
1028afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1029b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1030e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1031e7ff783cSmatthias.ringwald     l2cap_run();
103284836b65Smatthias.ringwald }
103384836b65Smatthias.ringwald 
10342b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
10354cf56b4aSmatthias.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."
10362b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
10372b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
10382b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
10392b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
10402b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
10412b360848Smatthias.ringwald         signaling_responses_pending++;
10422b360848Smatthias.ringwald         l2cap_run();
10432b360848Smatthias.ringwald     }
10442b360848Smatthias.ringwald }
10452b360848Smatthias.ringwald 
1046b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1047645658c9Smatthias.ringwald 
10489da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1049645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1050645658c9Smatthias.ringwald     if (!service) {
1051645658c9Smatthias.ringwald         // 0x0002 PSM not supported
10522b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1053645658c9Smatthias.ringwald         return;
1054645658c9Smatthias.ringwald     }
1055645658c9Smatthias.ringwald 
10565061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1057645658c9Smatthias.ringwald     if (!hci_connection) {
10582b360848Smatthias.ringwald         //
10599da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1060645658c9Smatthias.ringwald         return;
1061645658c9Smatthias.ringwald     }
10622bd8b7e7S[email protected] 
1063645658c9Smatthias.ringwald     // alloc structure
10649da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1065bb69aaaeS[email protected]     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
10662b360848Smatthias.ringwald     if (!channel){
10672b360848Smatthias.ringwald         // 0x0004 No resources available
10682b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10692b360848Smatthias.ringwald         return;
10702b360848Smatthias.ringwald     }
1071c523d53dS[email protected]     // Init memory (make valgrind happy)
1072c523d53dS[email protected]     memset(channel, 0, sizeof(l2cap_channel_t));
1073645658c9Smatthias.ringwald     // fill in
1074169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
1075169f8b28Smatthias.ringwald     channel->psm = psm;
1076169f8b28Smatthias.ringwald     channel->handle = handle;
1077f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1078b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1079b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1080fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
10810a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1082b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1083df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1084645658c9Smatthias.ringwald 
1085f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
10862985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
10872985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
10889775e25bSmatthias.ringwald     }
10899775e25bSmatthias.ringwald 
1090645658c9Smatthias.ringwald     // set initial state
1091df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1092ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1093e405ae81Smatthias.ringwald 
1094645658c9Smatthias.ringwald     // add to connections list
1095665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1096645658c9Smatthias.ringwald 
1097f85a9399S[email protected]     // assert security requirements
10981eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1099e405ae81Smatthias.ringwald }
1100645658c9Smatthias.ringwald 
1101ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1102e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1103b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1104e405ae81Smatthias.ringwald     if (!channel) {
1105ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1106e405ae81Smatthias.ringwald         return;
1107e405ae81Smatthias.ringwald     }
1108e405ae81Smatthias.ringwald 
1109552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1110e405ae81Smatthias.ringwald 
1111552d92a1Smatthias.ringwald     // process
1112552d92a1Smatthias.ringwald     l2cap_run();
1113e405ae81Smatthias.ringwald }
1114645658c9Smatthias.ringwald 
1115ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){
1116e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1117b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1118e405ae81Smatthias.ringwald     if (!channel) {
1119ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1120e405ae81Smatthias.ringwald         return;
1121e405ae81Smatthias.ringwald     }
1122e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1123e7ff783cSmatthias.ringwald     channel->reason = reason;
1124e7ff783cSmatthias.ringwald     l2cap_run();
1125645658c9Smatthias.ringwald }
1126645658c9Smatthias.ringwald 
11277f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1128b1988dceSmatthias.ringwald 
1129b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1130b1988dceSmatthias.ringwald 
1131f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
113263a7246aSmatthias.ringwald     if (flags & 1) {
113363a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
113463a7246aSmatthias.ringwald     }
113563a7246aSmatthias.ringwald 
11362784b77dSmatthias.ringwald     // accept the other's configuration options
1137f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
11383de7c0caSmatthias.ringwald     uint16_t pos     = 8;
11393de7c0caSmatthias.ringwald     while (pos < end_pos){
114063a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
114163a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
114263a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
114363a7246aSmatthias.ringwald         pos++;
11441dc511deSmatthias.ringwald         uint8_t length = command[pos++];
11451dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
114663a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1147f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
11489da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
114963a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
115063a7246aSmatthias.ringwald         }
11510fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11520fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1153f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
11540fe7a9d0S[email protected]         }
115563a7246aSmatthias.ringwald         // check for unknown options
115663a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1157c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
115863a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11591dc511deSmatthias.ringwald         }
11601dc511deSmatthias.ringwald         pos += length;
11611dc511deSmatthias.ringwald     }
11622784b77dSmatthias.ringwald }
11632784b77dSmatthias.ringwald 
1164fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11659da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
116673cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
116773cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1168019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1169019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1170fa8473a4Smatthias.ringwald     return 1;
1171fa8473a4Smatthias.ringwald }
1172fa8473a4Smatthias.ringwald 
1173fa8473a4Smatthias.ringwald 
11747f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11751e6aba47Smatthias.ringwald 
117600d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
117700d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
117838e5900eSmatthias.ringwald     uint16_t result = 0;
11791e6aba47Smatthias.ringwald 
11809da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1181b35f641cSmatthias.ringwald 
11829a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
11839a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
11849a011532Smatthias.ringwald         switch (channel->state){
1185fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
11869a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
11872b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
11889a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
11899a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
11909a011532Smatthias.ringwald                 break;
11919a011532Smatthias.ringwald 
11929a011532Smatthias.ringwald             default:
11939a011532Smatthias.ringwald                 // ignore in other states
11949a011532Smatthias.ringwald                 break;
11959a011532Smatthias.ringwald         }
11969a011532Smatthias.ringwald         return;
11979a011532Smatthias.ringwald     }
11989a011532Smatthias.ringwald 
119956081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
12001e6aba47Smatthias.ringwald     switch (channel->state) {
12011e6aba47Smatthias.ringwald 
12021e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
12031e6aba47Smatthias.ringwald             switch (code){
12041e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
12055932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1206f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
120738e5900eSmatthias.ringwald                     switch (result) {
120838e5900eSmatthias.ringwald                         case 0:
1209169f8b28Smatthias.ringwald                             // successful connection
1210f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1211fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
121228ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
121338e5900eSmatthias.ringwald                             break;
121438e5900eSmatthias.ringwald                         case 1:
12155932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
12165932bd7cS[email protected]                             l2cap_start_ertx(channel);
121738e5900eSmatthias.ringwald                             break;
121838e5900eSmatthias.ringwald                         default:
1219eb920dbeSmatthias.ringwald                             // channel closed
1220eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1221f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
122238e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1223eb920dbeSmatthias.ringwald 
1224eb920dbeSmatthias.ringwald                             // drop link key if security block
1225eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
12262e77e513S[email protected]                                 hci_drop_link_key_for_bd_addr(channel->address);
1227eb920dbeSmatthias.ringwald                             }
1228eb920dbeSmatthias.ringwald 
1229eb920dbeSmatthias.ringwald                             // discard channel
1230665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1231d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
123238e5900eSmatthias.ringwald                             break;
12331e6aba47Smatthias.ringwald                     }
12341e6aba47Smatthias.ringwald                     break;
123538e5900eSmatthias.ringwald 
123638e5900eSmatthias.ringwald                 default:
12371e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
123838e5900eSmatthias.ringwald                     break;
12391e6aba47Smatthias.ringwald             }
12401e6aba47Smatthias.ringwald             break;
12411e6aba47Smatthias.ringwald 
1242fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1243f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1244ae280e73Smatthias.ringwald             switch (code) {
1245ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
124628ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1247ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
124863a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
124963a7246aSmatthias.ringwald                         // only done if continuation not set
125063a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
125163a7246aSmatthias.ringwald                     }
1252ae280e73Smatthias.ringwald                     break;
12531e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12545932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12555932bd7cS[email protected]                     switch (result){
12565932bd7cS[email protected]                         case 0: // success
12575932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12585932bd7cS[email protected]                             break;
12595932bd7cS[email protected]                         case 4: // pending
12605932bd7cS[email protected]                             l2cap_start_ertx(channel);
12615932bd7cS[email protected]                             break;
12625932bd7cS[email protected]                         default:
1263fe9d8984S[email protected]                             // retry on negative result
1264fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1265fe9d8984S[email protected]                             break;
1266fe9d8984S[email protected]                     }
12675a67bd4aSmatthias.ringwald                     break;
12685a67bd4aSmatthias.ringwald                 default:
12695a67bd4aSmatthias.ringwald                     break;
12701e6aba47Smatthias.ringwald             }
1271fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1272fa8473a4Smatthias.ringwald                 // for open:
12735a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1274fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1275c8e4258aSmatthias.ringwald             }
1276c8e4258aSmatthias.ringwald             break;
1277f62db1e3Smatthias.ringwald 
1278f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1279f62db1e3Smatthias.ringwald             switch (code) {
1280f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
128127a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
128227a923d0Smatthias.ringwald                     break;
12835a67bd4aSmatthias.ringwald                 default:
12845a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
12855a67bd4aSmatthias.ringwald                     break;
128627a923d0Smatthias.ringwald             }
128727a923d0Smatthias.ringwald             break;
128884836b65Smatthias.ringwald 
128984836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
129084836b65Smatthias.ringwald             // @TODO handle incoming requests
129184836b65Smatthias.ringwald             break;
129284836b65Smatthias.ringwald 
129384836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
129484836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
129584836b65Smatthias.ringwald             break;
129610642e45Smatthias.ringwald         default:
129710642e45Smatthias.ringwald             break;
129827a923d0Smatthias.ringwald     }
12999da54300S[email protected]     // log_info("new state %u", channel->state);
130027a923d0Smatthias.ringwald }
130127a923d0Smatthias.ringwald 
130200d93d79Smatthias.ringwald 
13037f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
130400d93d79Smatthias.ringwald 
130500d93d79Smatthias.ringwald     // get code, signalind identifier and command len
130600d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
130700d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
130800d93d79Smatthias.ringwald 
130900d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
131000d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
131163a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
131200d93d79Smatthias.ringwald         return;
131300d93d79Smatthias.ringwald     }
131400d93d79Smatthias.ringwald 
131500d93d79Smatthias.ringwald     // general commands without an assigned channel
131600d93d79Smatthias.ringwald     switch(code) {
131700d93d79Smatthias.ringwald 
131800d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1319f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1320f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
132100d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
13222b83fb7dSmatthias.ringwald             return;
132300d93d79Smatthias.ringwald         }
132400d93d79Smatthias.ringwald 
13252b360848Smatthias.ringwald         case ECHO_REQUEST:
13262b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
13272b83fb7dSmatthias.ringwald             return;
132800d93d79Smatthias.ringwald 
132900d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1330f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
13312b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
13322b83fb7dSmatthias.ringwald             return;
133300d93d79Smatthias.ringwald         }
133400d93d79Smatthias.ringwald 
133500d93d79Smatthias.ringwald         default:
133600d93d79Smatthias.ringwald             break;
133700d93d79Smatthias.ringwald     }
133800d93d79Smatthias.ringwald 
133900d93d79Smatthias.ringwald 
134000d93d79Smatthias.ringwald     // Get potential destination CID
1341f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
134200d93d79Smatthias.ringwald 
134300d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1344665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1345665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1346665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1347665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1348c22aecc9S[email protected]         if (channel->handle != handle) continue;
134900d93d79Smatthias.ringwald         if (code & 1) {
1350b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1351b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
135200d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13534e32727eSmatthias.ringwald                 break;
135400d93d79Smatthias.ringwald             }
135500d93d79Smatthias.ringwald         } else {
1356b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
135700d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
135800d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13594e32727eSmatthias.ringwald                 break;
136000d93d79Smatthias.ringwald             }
136100d93d79Smatthias.ringwald         }
136200d93d79Smatthias.ringwald     }
136300d93d79Smatthias.ringwald }
136400d93d79Smatthias.ringwald 
1365d9a7306aSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ){
136600d93d79Smatthias.ringwald 
136700d93d79Smatthias.ringwald     // Get Channel ID
136800d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
136900d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
137000d93d79Smatthias.ringwald 
13715652b5ffS[email protected]     switch (channel_id) {
13725652b5ffS[email protected] 
13735652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13745652b5ffS[email protected] 
137500d93d79Smatthias.ringwald             uint16_t command_offset = 8;
137600d93d79Smatthias.ringwald             while (command_offset < size) {
137700d93d79Smatthias.ringwald 
137800d93d79Smatthias.ringwald                 // handle signaling commands
137900d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
138000d93d79Smatthias.ringwald 
138100d93d79Smatthias.ringwald                 // increment command_offset
1382f8fbdce0SMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
138300d93d79Smatthias.ringwald             }
13845652b5ffS[email protected]             break;
138500d93d79Smatthias.ringwald         }
138600d93d79Smatthias.ringwald 
13875652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
13885628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
13895628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13905652b5ffS[email protected]             }
13915652b5ffS[email protected]             break;
13925652b5ffS[email protected] 
13935652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
13945628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
13955628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13965652b5ffS[email protected]             }
13975652b5ffS[email protected]             break;
13985652b5ffS[email protected] 
1399462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
14005628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
14015628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1402462e630dS[email protected]             }
1403462e630dS[email protected]             break;
1404462e630dS[email protected] 
14051bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1406ccf076adS[email protected]             switch (packet[8]){
1407ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1408f8fbdce0SMatthias Ringwald                     uint16_t result = little_endian_read_16(packet, 12);
1409ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1410ccf076adS[email protected]                     break;
1411ccf076adS[email protected]                 }
1412ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1413ccf076adS[email protected]                     uint8_t event[10];
1414ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1415ccf076adS[email protected]                     event[1] = 8;
1416ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1417da886c03S[email protected] 
1418da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1419da886c03S[email protected]                     if (connection){
14206d91fb6cSMatthias Ringwald                         if (connection->role != HCI_ROLE_MASTER){
14216d91fb6cSMatthias Ringwald                             // reject command without notifying upper layer when not in master role
14226d91fb6cSMatthias Ringwald                             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14236d91fb6cSMatthias Ringwald                             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14246d91fb6cSMatthias Ringwald                             break;
14256d91fb6cSMatthias Ringwald                         }
1426da886c03S[email protected]                         int update_parameter = 1;
1427a4c06b28SMatthias Ringwald                         le_connection_parameter_range_t existing_range;
1428a4c06b28SMatthias Ringwald                         gap_le_get_connection_parameter_range(existing_range);
1429f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_min = little_endian_read_16(packet,12);
1430f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_max = little_endian_read_16(packet,14);
1431f8fbdce0SMatthias Ringwald                         uint16_t le_conn_latency = little_endian_read_16(packet,16);
1432f8fbdce0SMatthias Ringwald                         uint16_t le_supervision_timeout = little_endian_read_16(packet,18);
1433da886c03S[email protected] 
1434da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1435da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1436da886c03S[email protected] 
1437da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1438da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1439da886c03S[email protected] 
1440da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1441da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1442da886c03S[email protected] 
1443da886c03S[email protected]                         if (update_parameter){
1444da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1445da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1446da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1447da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1448da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1449da886c03S[email protected]                         } else {
1450da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1451da886c03S[email protected]                         }
145279f53f1dSMatthias Ringwald                         connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1];
1453da886c03S[email protected]                     }
1454da886c03S[email protected] 
1455ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
145633c40538SMatthias Ringwald                     if (!l2cap_event_packet_handler) break;
145733c40538SMatthias Ringwald                     (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1458ccf076adS[email protected]                     break;
1459ccf076adS[email protected]                 }
1460ccf076adS[email protected]                 default: {
14611bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14621bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14631bbc0b23S[email protected]                     break;
14641bbc0b23S[email protected]                 }
1465ccf076adS[email protected]             }
1466ccf076adS[email protected]             break;
1467ccf076adS[email protected]         }
14681bbc0b23S[email protected] 
14695652b5ffS[email protected]         default: {
147000d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
147100d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
147200d93d79Smatthias.ringwald             if (channel) {
147317a9b554SMatthias Ringwald                 l2cap_dispatch_to_channel(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
147400d93d79Smatthias.ringwald             }
14755652b5ffS[email protected]             break;
14765652b5ffS[email protected]         }
14775652b5ffS[email protected]     }
147800d93d79Smatthias.ringwald 
14791eb2563eS[email protected]     l2cap_run();
14802718e2e7Smatthias.ringwald }
148100d93d79Smatthias.ringwald 
148215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
148327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1484f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1485f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1486f62db1e3Smatthias.ringwald     // discard channel
14879dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1488665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1489d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1490c8e4258aSmatthias.ringwald }
14911e6aba47Smatthias.ringwald 
14928f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1493665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1494665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1495665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1496665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
14979d9bbc01Smatthias.ringwald         if ( service->psm == psm){
14989d9bbc01Smatthias.ringwald             return service;
14999d9bbc01Smatthias.ringwald         };
15009d9bbc01Smatthias.ringwald     }
15019d9bbc01Smatthias.ringwald     return NULL;
15029d9bbc01Smatthias.ringwald }
15039d9bbc01Smatthias.ringwald 
15047192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
15057192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
15067192e786SMatthias Ringwald }
15077192e786SMatthias Ringwald 
1508e0abb8e7S[email protected] 
1509be2053a6SMatthias 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){
1510be2053a6SMatthias Ringwald 
1511be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1512e0abb8e7S[email protected] 
15134bb582b6Smatthias.ringwald     // check for alread registered psm
15144bb582b6Smatthias.ringwald     // TODO: emit error event
15159d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1516277abc2cSmatthias.ringwald     if (service) {
1517be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1518be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1519277abc2cSmatthias.ringwald     }
15209d9bbc01Smatthias.ringwald 
15214bb582b6Smatthias.ringwald     // alloc structure
15224bb582b6Smatthias.ringwald     // TODO: emit error event
1523bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1524277abc2cSmatthias.ringwald     if (!service) {
1525be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1526be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1527277abc2cSmatthias.ringwald     }
15289d9bbc01Smatthias.ringwald 
15299d9bbc01Smatthias.ringwald     // fill in
15309d9bbc01Smatthias.ringwald     service->psm = psm;
15319d9bbc01Smatthias.ringwald     service->mtu = mtu;
153205ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1533df3354fcS[email protected]     service->required_security_level = security_level;
15349d9bbc01Smatthias.ringwald 
15359d9bbc01Smatthias.ringwald     // add to services list
1536665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1537c0e866bfSmatthias.ringwald 
1538c0e866bfSmatthias.ringwald     // enable page scan
1539c0e866bfSmatthias.ringwald     hci_connectable_control(1);
15405842b6d9Smatthias.ringwald 
1541be2053a6SMatthias Ringwald     return 0;
15429d9bbc01Smatthias.ringwald }
15439d9bbc01Smatthias.ringwald 
154402f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){
1545e0abb8e7S[email protected] 
1546e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1547e0abb8e7S[email protected] 
15489d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1549037d6e48Smatthias.ringwald     if (!service) return;
1550665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1551d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1552c0e866bfSmatthias.ringwald 
1553c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1554665d90f2SMatthias Ringwald     if (!btstack_linked_list_empty(&l2cap_services)) return;
1555c0e866bfSmatthias.ringwald     hci_connectable_control(0);
15569d9bbc01Smatthias.ringwald }
15579d9bbc01Smatthias.ringwald 
15585652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
155905ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
15605628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
15615628cf69SMatthias Ringwald     if (index < 0) return;
15625628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
15635652b5ffS[email protected] }
15645652b5ffS[email protected] 
1565a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
15667192e786SMatthias Ringwald 
15677f02f414SMatthias Ringwald 
15687f02f414SMatthias Ringwald #if 0
15697192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
15707192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
15717192e786SMatthias Ringwald }
15727192e786SMatthias Ringwald /**
15737192e786SMatthias Ringwald  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
15747192e786SMatthias Ringwald  * @param
15757192e786SMatthias Ringwald  */
1576ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm,
15777192e786SMatthias Ringwald     uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){
15787192e786SMatthias Ringwald 
15797192e786SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection);
15807192e786SMatthias Ringwald 
15817192e786SMatthias Ringwald     // check for alread registered psm
15827192e786SMatthias Ringwald     // TODO: emit error event
15837192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
15847192e786SMatthias Ringwald     if (service) {
15857192e786SMatthias Ringwald         log_error("l2cap_le_register_service_internal: PSM %u already registered", psm);
15867192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
15877192e786SMatthias Ringwald         return;
15887192e786SMatthias Ringwald     }
15897192e786SMatthias Ringwald 
15907192e786SMatthias Ringwald     // alloc structure
15917192e786SMatthias Ringwald     // TODO: emit error event
15927192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
15937192e786SMatthias Ringwald     if (!service) {
15947192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
15957192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
15967192e786SMatthias Ringwald         return;
15977192e786SMatthias Ringwald     }
15987192e786SMatthias Ringwald 
15997192e786SMatthias Ringwald     // fill in
16007192e786SMatthias Ringwald     service->psm = psm;
16017192e786SMatthias Ringwald     service->mtu = mtu;
16027192e786SMatthias Ringwald     service->mps = mps;
16037192e786SMatthias Ringwald     service->packet_handler = packet_handler;
16047192e786SMatthias Ringwald     service->required_security_level = security_level;
16057192e786SMatthias Ringwald 
16067192e786SMatthias Ringwald     // add to services list
1607665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
16087192e786SMatthias Ringwald 
16097192e786SMatthias Ringwald     // done
16107192e786SMatthias Ringwald     l2cap_emit_service_registered(connection, 0, psm);
16117192e786SMatthias Ringwald }
16127192e786SMatthias Ringwald 
1613ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) {
16147192e786SMatthias Ringwald 
16157192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
16167192e786SMatthias Ringwald 
16177192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
16187192e786SMatthias Ringwald     if (!service) return;
1619665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
16207192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
16217192e786SMatthias Ringwald }
16227192e786SMatthias Ringwald #endif
16237f02f414SMatthias Ringwald #endif
1624