xref: /btstack/src/l2cap.c (revision 5628cf69b5a59a9cf27711ff0736732db872c35a)
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 
69*5628cf69SMatthias Ringwald // internal table
70*5628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
71*5628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
72*5628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
73*5628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
74*5628cf69SMatthias 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 
86*5628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
87*5628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
88*5628cf69SMatthias Ringwald     // uint8_t waiting_for_can_send_now;
89*5628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
90*5628cf69SMatthias Ringwald 
91*5628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
92*5628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
93*5628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
94*5628cf69SMatthias 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;
105*5628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
10639bda6d5Smatthias.ringwald 
107*5628cf69SMatthias Ringwald // static btstack_packet_handler_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL];
108*5628cf69SMatthias Ringwald // static btstack_packet_handler_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL];
109*5628cf69SMatthias Ringwald // static btstack_packet_handler_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL];
110*5628cf69SMatthias Ringwald 
111*5628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
112*5628cf69SMatthias Ringwald     switch (channel_id){
113*5628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
114*5628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
115*5628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
116*5628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
117*5628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
118*5628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
119*5628cf69SMatthias Ringwald         default:
120*5628cf69SMatthias Ringwald             return -1;
121*5628cf69SMatthias Ringwald         }
122*5628cf69SMatthias 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;
133*5628cf69SMatthias 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 
2626cd4da6bS[email protected] int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle){
2636cd4da6bS[email protected]     return hci_can_send_acl_packet_now(handle);
2643cab4fcaS[email protected] }
2653cab4fcaS[email protected] 
26696cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
26796cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
26896cbd662Smatthias.ringwald     if (channel) {
26996cbd662Smatthias.ringwald         return channel->remote_mtu;
27096cbd662Smatthias.ringwald     }
27196cbd662Smatthias.ringwald     return 0;
27296cbd662Smatthias.ringwald }
27396cbd662Smatthias.ringwald 
274ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
275665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
276665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
277665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
278665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2795932bd7cS[email protected]         if ( &channel->rtx == ts) {
2805932bd7cS[email protected]             return channel;
2815932bd7cS[email protected]         }
2825932bd7cS[email protected]     }
2835932bd7cS[email protected]     return NULL;
2845932bd7cS[email protected] }
2855932bd7cS[email protected] 
286ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
2875932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2885932bd7cS[email protected]     if (!ts) return;
2895932bd7cS[email protected] 
2905932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2915932bd7cS[email protected] 
2925932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
2935932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
2945932bd7cS[email protected]     // notify client
2955932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
2965932bd7cS[email protected] 
2975932bd7cS[email protected]     // discard channel
2989dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
299665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3005932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3015932bd7cS[email protected] }
3025932bd7cS[email protected] 
3035932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3045932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
305528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3065932bd7cS[email protected] }
3075932bd7cS[email protected] 
3085932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3095932bd7cS[email protected]     l2cap_stop_rtx(channel);
310cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
311528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
312528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
313528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3145932bd7cS[email protected] }
3155932bd7cS[email protected] 
3165932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3175932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3185932bd7cS[email protected]     l2cap_stop_rtx(channel);
319528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
320528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
321528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3225932bd7cS[email protected] }
3235932bd7cS[email protected] 
32471de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
325ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
326ac301f95S[email protected] }
327ac301f95S[email protected] 
328df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
329ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
330df3354fcS[email protected] }
3315932bd7cS[email protected] 
3327f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
333b1d43497Smatthias.ringwald 
334a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3359da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
336b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
337b1d43497Smatthias.ringwald     }
338b1d43497Smatthias.ringwald 
3399da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3402a373862S[email protected]     hci_reserve_packet_buffer();
341facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
34258de5610Smatthias.ringwald     va_list argptr;
34358de5610Smatthias.ringwald     va_start(argptr, identifier);
34470efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
34558de5610Smatthias.ringwald     va_end(argptr);
3469da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
347826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
34858de5610Smatthias.ringwald }
34958de5610Smatthias.ringwald 
350a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3517f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
35270efece1S[email protected] 
353a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3549da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
35570efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
35670efece1S[email protected]     }
35770efece1S[email protected] 
3589da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3592a373862S[email protected]     hci_reserve_packet_buffer();
360facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
36170efece1S[email protected]     va_list argptr;
36270efece1S[email protected]     va_start(argptr, identifier);
36370efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
36470efece1S[email protected]     va_end(argptr);
3659da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
366826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
36770efece1S[email protected] }
36870efece1S[email protected] #endif
36970efece1S[email protected] 
370b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
371facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
372b1d43497Smatthias.ringwald }
3736218e6f1Smatthias.ringwald 
3746b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3756b4af23dS[email protected]     return hci_reserve_packet_buffer();
3766b4af23dS[email protected] }
3776b4af23dS[email protected] 
37868a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
37968a0fcf7S[email protected]     hci_release_packet_buffer();
38068a0fcf7S[email protected] }
38168a0fcf7S[email protected] 
38268a0fcf7S[email protected] 
383b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
384b1d43497Smatthias.ringwald 
385c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
386c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
387c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
388c8b9416aS[email protected]     }
389c8b9416aS[email protected] 
39058de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
391b1d43497Smatthias.ringwald     if (!channel) {
3929da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
393b1d43497Smatthias.ringwald         return -1;   // TODO: define error
3946218e6f1Smatthias.ringwald     }
3956218e6f1Smatthias.ringwald 
396a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(channel->handle)){
3979da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
398a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
399a35252c8S[email protected]     }
400a35252c8S[email protected] 
4013aff022fSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle);
402b1d43497Smatthias.ringwald 
403facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
404b1d43497Smatthias.ringwald 
405e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
406e9772277S[email protected] 
407e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
408f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14));
40958de5610Smatthias.ringwald     // 2 - ACL length
410f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
41158de5610Smatthias.ringwald     // 4 - L2CAP packet length
412f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
41358de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
414f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
41558de5610Smatthias.ringwald     // send
416826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
41791b99603Smatthias.ringwald 
4186218e6f1Smatthias.ringwald     return err;
41958de5610Smatthias.ringwald }
42058de5610Smatthias.ringwald 
4212149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
4222149f12eSmatthias.ringwald 
423c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
424c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4252149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4262149f12eSmatthias.ringwald     }
4272149f12eSmatthias.ringwald 
428a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(handle)){
4299da54300S[email protected]         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid);
430c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
431c8b9416aS[email protected]     }
432c8b9416aS[email protected] 
4339da54300S[email protected]     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid);
4342149f12eSmatthias.ringwald 
435facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4362149f12eSmatthias.ringwald 
437e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
438e9772277S[email protected] 
439e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
440f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
4412149f12eSmatthias.ringwald     // 2 - ACL length
442f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4432149f12eSmatthias.ringwald     // 4 - L2CAP packet length
444f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4452149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
446f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4472149f12eSmatthias.ringwald     // send
448826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4492149f12eSmatthias.ringwald 
4502149f12eSmatthias.ringwald     return err;
4512149f12eSmatthias.ringwald }
4522149f12eSmatthias.ringwald 
453ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
454b1d43497Smatthias.ringwald 
455a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
456a35252c8S[email protected]     if (!channel) {
457ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
458a35252c8S[email protected]         return -1;   // TODO: define error
459a35252c8S[email protected]     }
460a35252c8S[email protected] 
461f0efaa57S[email protected]     if (len > channel->remote_mtu){
462ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
463f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
464f0efaa57S[email protected]     }
465f0efaa57S[email protected] 
466a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(channel->handle)){
467ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
468b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
469b1d43497Smatthias.ringwald     }
470b1d43497Smatthias.ringwald 
4712a373862S[email protected]     hci_reserve_packet_buffer();
472facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
473b1d43497Smatthias.ringwald 
474b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
475b1d43497Smatthias.ringwald 
476b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
477b1d43497Smatthias.ringwald }
478b1d43497Smatthias.ringwald 
4792149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
4802149f12eSmatthias.ringwald 
481a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
482ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
4832149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4842149f12eSmatthias.ringwald     }
4852149f12eSmatthias.ringwald 
4862a373862S[email protected]     hci_reserve_packet_buffer();
487facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4882149f12eSmatthias.ringwald 
4892149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4902149f12eSmatthias.ringwald 
4912149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4922149f12eSmatthias.ringwald }
4932149f12eSmatthias.ringwald 
4940e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){
4950e37e417S[email protected]     return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data);
4960e37e417S[email protected] }
4970e37e417S[email protected] 
49828ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
49928ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
50028ca2b46S[email protected] }
50128ca2b46S[email protected] 
50228ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
50328ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
50428ca2b46S[email protected] }
50528ca2b46S[email protected] 
50628ca2b46S[email protected] 
507b1d43497Smatthias.ringwald 
5088158c421Smatthias.ringwald // MARK: L2CAP_RUN
5092cd0be45Smatthias.ringwald // process outstanding signaling tasks
5107f02f414SMatthias Ringwald static void l2cap_run(void){
5112b83fb7dSmatthias.ringwald 
51222c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
51322c29ab4SMatthias Ringwald 
5142b83fb7dSmatthias.ringwald     // check pending signaling responses
5152b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5162b83fb7dSmatthias.ringwald 
5172b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
518a35252c8S[email protected] 
519a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
520a35252c8S[email protected] 
5212b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5222b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
52363a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
524f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5252b83fb7dSmatthias.ringwald 
526f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
527f53da564S[email protected]         signaling_responses_pending--;
528f53da564S[email protected]         int i;
529f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
530f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
531f53da564S[email protected]         }
532f53da564S[email protected] 
533f53da564S[email protected]         switch (response_code){
5342b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5352b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5362bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5374d816277S[email protected]                 if (result == 0x0003){
5382bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5394d816277S[email protected]                 }
5402b360848Smatthias.ringwald                 break;
5412b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5422b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5432b83fb7dSmatthias.ringwald                 break;
5442b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5453b0484b3S[email protected]                 switch (infoType){
5463b0484b3S[email protected]                     case 1: { // Connectionless MTU
5473b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5483b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5493b0484b3S[email protected]                         break;
5503b0484b3S[email protected]                     }
5513b0484b3S[email protected]                     case 2: { // Extended Features Supported
552462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
553462e630dS[email protected]                         uint32_t features = 0x280;
5543b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5553b0484b3S[email protected]                         break;
5563b0484b3S[email protected]                     }
5573b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5583b0484b3S[email protected]                         uint8_t map[8];
5593b0484b3S[email protected]                         memset(map, 0, 8);
560462e630dS[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02)
5613b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5623b0484b3S[email protected]                         break;
5633b0484b3S[email protected]                     }
5643b0484b3S[email protected]                     default:
5652b83fb7dSmatthias.ringwald                         // all other types are not supported
5662b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5673b0484b3S[email protected]                         break;
5682b83fb7dSmatthias.ringwald                 }
5692b83fb7dSmatthias.ringwald                 break;
57063a7246aSmatthias.ringwald             case COMMAND_REJECT:
5715ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
572a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
57370efece1S[email protected]             case COMMAND_REJECT_LE:
57470efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
57563a7246aSmatthias.ringwald                 break;
57670efece1S[email protected] #endif
5772b83fb7dSmatthias.ringwald             default:
5782b83fb7dSmatthias.ringwald                 // should not happen
5792b83fb7dSmatthias.ringwald                 break;
5802b83fb7dSmatthias.ringwald         }
5812b83fb7dSmatthias.ringwald     }
5822b83fb7dSmatthias.ringwald 
583ae280e73Smatthias.ringwald     uint8_t  config_options[4];
584665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
585665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
586665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
587baf94f06S[email protected] 
588665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
58922c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
5902cd0be45Smatthias.ringwald         switch (channel->state){
5912cd0be45Smatthias.ringwald 
592df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
593ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
594baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
595a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
596ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
597a00031e2S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
598ad671560S[email protected]                 }
599ad671560S[email protected]                 break;
600ad671560S[email protected] 
60102b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
602baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
60364472d52Smatthias.ringwald                 // send connection request - set state first
60464472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
60502b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6068f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
60702b22dc4Smatthias.ringwald                 break;
60802b22dc4Smatthias.ringwald 
609e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
610baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
61122c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
6121eb2563eS[email protected]                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
613e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6149dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
615665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
616d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
617e7ff783cSmatthias.ringwald                 break;
618e7ff783cSmatthias.ringwald 
619552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
620baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
621fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
62228ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
6232a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
624552d92a1Smatthias.ringwald                 break;
625552d92a1Smatthias.ringwald 
6266fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
627baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
6286fdcc387Smatthias.ringwald                 // success, start l2cap handshake
629b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6306fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
6312a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6325932bd7cS[email protected]                 l2cap_start_rtx(channel);
6336fdcc387Smatthias.ringwald                 break;
6346fdcc387Smatthias.ringwald 
635fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
636baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
63773cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
63863a7246aSmatthias.ringwald                     uint16_t flags = 0;
63928ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
64063a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
64163a7246aSmatthias.ringwald                         flags = 1;
64263a7246aSmatthias.ringwald                     } else {
64328ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
64463a7246aSmatthias.ringwald                     }
64563a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
64663a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
64763a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
64863a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
64963a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
650f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
65163a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
65263a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
65363a7246aSmatthias.ringwald                     } else {
65463a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
65563a7246aSmatthias.ringwald                     }
65663a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
657fa8473a4Smatthias.ringwald                 }
65873cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
65928ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
66028ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
661b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
662ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
663ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
664f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
665b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6665932bd7cS[email protected]                     l2cap_start_rtx(channel);
667fa8473a4Smatthias.ringwald                 }
668fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
669552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
670552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
671fa8473a4Smatthias.ringwald                 }
672552d92a1Smatthias.ringwald                 break;
673552d92a1Smatthias.ringwald 
674e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
675baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
67622c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
677b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6785932bd7cS[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 :)
679756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
680e7ff783cSmatthias.ringwald                 break;
681e7ff783cSmatthias.ringwald 
682e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
683baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
684b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6852cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
6862a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
6872cd0be45Smatthias.ringwald                 break;
6882cd0be45Smatthias.ringwald             default:
6892cd0be45Smatthias.ringwald                 break;
6902cd0be45Smatthias.ringwald         }
6912cd0be45Smatthias.ringwald     }
692da886c03S[email protected] 
693a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
694da886c03S[email protected]     // send l2cap con paramter update if necessary
695da886c03S[email protected]     hci_connections_get_iterator(&it);
696665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
697665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
6985d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
699b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
700da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
701b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
702b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
703b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
704b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
705b68d7bc3SMatthias Ringwald                 break;
706da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
707b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
708b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
709da886c03S[email protected]                 break;
710da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
711b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
712b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
713da886c03S[email protected]                 break;
714da886c03S[email protected]             default:
715da886c03S[email protected]                 break;
716da886c03S[email protected]         }
717da886c03S[email protected]     }
7184d7157c3S[email protected] #endif
719da886c03S[email protected] 
72022c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
7212cd0be45Smatthias.ringwald }
7222cd0be45Smatthias.ringwald 
7234aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7244ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
725fa8c92f6Smatthias.ringwald }
726fa8c92f6Smatthias.ringwald 
72771de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
7284ff786cfS[email protected]     return l2cap_max_mtu();
729e5e1518dS[email protected] }
730e5e1518dS[email protected] 
7312df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){
7322df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7335533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7342df5dadcS[email protected]         // success, start l2cap handshake
7352df5dadcS[email protected]         channel->handle = handle;
7362df5dadcS[email protected]         // check remote SSP feature first
7372df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7382df5dadcS[email protected]     }
7392df5dadcS[email protected] }
7402df5dadcS[email protected] 
7412df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7422df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7432df5dadcS[email protected] 
7442df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
745ac301f95S[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));
7462df5dadcS[email protected]     if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7472df5dadcS[email protected]         // request security level 2
7482df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
7491429b2d6S[email protected]         gap_request_security_level(channel->handle, LEVEL_2);
7502df5dadcS[email protected]         return;
7512df5dadcS[email protected]     }
7522df5dadcS[email protected]     // fine, go ahead
7532df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7542df5dadcS[email protected] }
7552df5dadcS[email protected] 
7569077cb15SMatthias Ringwald /**
7579077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
7589077cb15SMatthias Ringwald  * @param packet_handler
7599077cb15SMatthias Ringwald  * @param address
7609077cb15SMatthias Ringwald  * @param psm
7619077cb15SMatthias Ringwald  * @param mtu
7629077cb15SMatthias Ringwald  * @param local_cid
7639077cb15SMatthias Ringwald  */
7649077cb15SMatthias 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){
7659077cb15SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
7669077cb15SMatthias Ringwald 
7679077cb15SMatthias Ringwald     // alloc structure
7689077cb15SMatthias Ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
7699077cb15SMatthias Ringwald     if (!chan) {
7709077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
7719077cb15SMatthias Ringwald     }
7729077cb15SMatthias Ringwald 
7739077cb15SMatthias Ringwald      // Init memory (make valgrind happy)
7749077cb15SMatthias Ringwald     memset(chan, 0, sizeof(l2cap_channel_t));
7759077cb15SMatthias Ringwald     // limit local mtu to max acl packet length - l2cap header
7769077cb15SMatthias Ringwald     if (mtu > l2cap_max_mtu()) {
7779077cb15SMatthias Ringwald         mtu = l2cap_max_mtu();
7789077cb15SMatthias Ringwald     }
7799077cb15SMatthias Ringwald 
7809077cb15SMatthias Ringwald     // fill in
7819077cb15SMatthias Ringwald     BD_ADDR_COPY(chan->address, address);
7829077cb15SMatthias Ringwald     chan->psm = psm;
7839077cb15SMatthias Ringwald     chan->handle = 0;
7849077cb15SMatthias Ringwald     chan->packet_handler = channel_packet_handler;
7859077cb15SMatthias Ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
7869077cb15SMatthias Ringwald     chan->local_mtu = mtu;
7879077cb15SMatthias Ringwald     chan->local_cid = l2cap_next_local_cid();
7889077cb15SMatthias Ringwald 
7899077cb15SMatthias Ringwald     // set initial state
7909077cb15SMatthias Ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
7919077cb15SMatthias Ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
7929077cb15SMatthias Ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
7939077cb15SMatthias Ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
7949077cb15SMatthias Ringwald     chan->required_security_level = LEVEL_0;
7959077cb15SMatthias Ringwald 
7969077cb15SMatthias Ringwald     // add to connections list
797665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan);
7989077cb15SMatthias Ringwald 
7999077cb15SMatthias Ringwald     // store local_cid
8009077cb15SMatthias Ringwald     if (out_local_cid){
8019077cb15SMatthias Ringwald        *out_local_cid = chan->local_cid;
8029077cb15SMatthias Ringwald     }
8039077cb15SMatthias Ringwald 
8049077cb15SMatthias Ringwald     // check if hci connection is already usable
8059077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
8069077cb15SMatthias Ringwald     if (conn){
807add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
8089077cb15SMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, chan);
8099077cb15SMatthias Ringwald         // check if remote supported fearures are already received
8109077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
8119077cb15SMatthias Ringwald             l2cap_handle_remote_supported_features_received(chan);
8129077cb15SMatthias Ringwald         }
8139077cb15SMatthias Ringwald     }
8149077cb15SMatthias Ringwald 
8159077cb15SMatthias Ringwald     l2cap_run();
8169077cb15SMatthias Ringwald 
8179077cb15SMatthias Ringwald     return 0;
8189077cb15SMatthias Ringwald }
8199077cb15SMatthias Ringwald 
820ce8f182eSMatthias Ringwald void
821ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
822e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
823b35f641cSmatthias.ringwald     // find channel for local_cid
824b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
825f62db1e3Smatthias.ringwald     if (channel) {
826e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
827f62db1e3Smatthias.ringwald     }
8282cd0be45Smatthias.ringwald     // process
8292cd0be45Smatthias.ringwald     l2cap_run();
83043625864Smatthias.ringwald }
8311e6aba47Smatthias.ringwald 
832afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
833665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
834665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
835665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
836665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
837c22aecc9S[email protected]         if ( BD_ADDR_CMP( channel->address, address) != 0) continue;
838c22aecc9S[email protected]         // channel for this address found
839c22aecc9S[email protected]         switch (channel->state){
840c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
841c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
842afde0c52Smatthias.ringwald                 // failure, forward error code
843afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
844afde0c52Smatthias.ringwald                 // discard channel
8459dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
846665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
847d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
848c22aecc9S[email protected]                 break;
849c22aecc9S[email protected]             default:
850c22aecc9S[email protected]                 break;
851afde0c52Smatthias.ringwald         }
852afde0c52Smatthias.ringwald     }
853afde0c52Smatthias.ringwald }
854afde0c52Smatthias.ringwald 
855afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
856665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
857665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
858665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
859665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
860afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
8612df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
862afde0c52Smatthias.ringwald         }
863afde0c52Smatthias.ringwald     }
8646fdcc387Smatthias.ringwald     // process
8656fdcc387Smatthias.ringwald     l2cap_run();
866afde0c52Smatthias.ringwald }
867b448a0e7Smatthias.ringwald 
86833c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
86933c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
87033c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
87133c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
87233c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
87333c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
87433c40538SMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->handle)) continue;
87533c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
87633c40538SMatthias Ringwald         l2cap_emit_can_send_now(channel);
87733c40538SMatthias Ringwald     }
87833c40538SMatthias Ringwald }
87933c40538SMatthias Ringwald 
880d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
881afde0c52Smatthias.ringwald 
882afde0c52Smatthias.ringwald     bd_addr_t address;
883afde0c52Smatthias.ringwald     hci_con_handle_t handle;
884665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
8852d00edd4Smatthias.ringwald     int hci_con_used;
886afde0c52Smatthias.ringwald 
887afde0c52Smatthias.ringwald     switch(packet[0]){
888afde0c52Smatthias.ringwald 
889afde0c52Smatthias.ringwald         // handle connection complete events
890afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
891afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
892afde0c52Smatthias.ringwald             if (packet[2] == 0){
893f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
894afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
895afde0c52Smatthias.ringwald             } else {
896afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
897afde0c52Smatthias.ringwald             }
898afde0c52Smatthias.ringwald             break;
899afde0c52Smatthias.ringwald 
900afde0c52Smatthias.ringwald         // handle successful create connection cancel command
901afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
902afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
903afde0c52Smatthias.ringwald                 if (packet[5] == 0){
904afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
905afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
906afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
90703cfbabcSmatthias.ringwald                 }
9081e6aba47Smatthias.ringwald             }
90939d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
91039d59809Smatthias.ringwald             break;
91139d59809Smatthias.ringwald 
91239d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
91339d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
914afde0c52Smatthias.ringwald             break;
91527a923d0Smatthias.ringwald 
9161e6aba47Smatthias.ringwald         // handle disconnection complete events
917afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
918c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
919f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
920665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
921665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
922665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
923c22aecc9S[email protected]                 if (channel->handle != handle) continue;
92415ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
9259dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
926665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
927d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
92827a923d0Smatthias.ringwald             }
929afde0c52Smatthias.ringwald             break;
930fcadd0caSmatthias.ringwald 
93133c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
93233c40538SMatthias Ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
9336218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
93402b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
93533c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
9366218e6f1Smatthias.ringwald             break;
9376218e6f1Smatthias.ringwald 
938ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
939afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
940f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
941bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
94280ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
9432d00edd4Smatthias.ringwald             hci_con_used = 0;
944665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
945665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
946665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
947c22aecc9S[email protected]                 if (channel->handle != handle) continue;
9482d00edd4Smatthias.ringwald                 hci_con_used = 1;
949c22aecc9S[email protected]                 break;
950ee091cf1Smatthias.ringwald             }
9512d00edd4Smatthias.ringwald             if (hci_con_used) break;
952d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
9539edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
954afde0c52Smatthias.ringwald             break;
955ee091cf1Smatthias.ringwald 
956df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
957f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
958665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
959665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
960665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
961df3354fcS[email protected]                 if (channel->handle != handle) continue;
9622df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
963df3354fcS[email protected]                 break;
964df3354fcS[email protected]             }
965c22aecc9S[email protected]             break;
966df3354fcS[email protected] 
9675611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
968f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
969bd63148eS[email protected]             log_info("l2cap - security level update");
970665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
971665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
972665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
973f85a9399S[email protected]                 if (channel->handle != handle) continue;
9745533f01eS[email protected] 
975bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
976bd63148eS[email protected] 
977e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
9785533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
9795533f01eS[email protected] 
980df3354fcS[email protected]                 switch (channel->state){
981df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
9825533f01eS[email protected]                         if (actual_level >= required_level){
983f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
984f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
9851eb2563eS[email protected]                         } else {
986775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
9871eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
9881eb2563eS[email protected]                         }
989df3354fcS[email protected]                         break;
990df3354fcS[email protected] 
991df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
9925533f01eS[email protected]                         if (actual_level >= required_level){
993df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
994df3354fcS[email protected]                         } else {
995df3354fcS[email protected]                             // disconnnect, authentication not good enough
996df3354fcS[email protected]                             hci_disconnect_security_block(handle);
997df3354fcS[email protected]                         }
998df3354fcS[email protected]                         break;
999df3354fcS[email protected] 
1000df3354fcS[email protected]                     default:
1001df3354fcS[email protected]                         break;
1002df3354fcS[email protected]                 }
1003f85a9399S[email protected]             }
1004f85a9399S[email protected]             break;
1005f85a9399S[email protected] 
1006afde0c52Smatthias.ringwald         default:
1007afde0c52Smatthias.ringwald             break;
1008afde0c52Smatthias.ringwald     }
1009afde0c52Smatthias.ringwald 
1010bd63148eS[email protected]     l2cap_run();
10111e6aba47Smatthias.ringwald }
10121e6aba47Smatthias.ringwald 
1013afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1014b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1015e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1016e7ff783cSmatthias.ringwald     l2cap_run();
101784836b65Smatthias.ringwald }
101884836b65Smatthias.ringwald 
10192b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
10204cf56b4aSmatthias.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."
10212b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
10222b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
10232b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
10242b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
10252b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
10262b360848Smatthias.ringwald         signaling_responses_pending++;
10272b360848Smatthias.ringwald         l2cap_run();
10282b360848Smatthias.ringwald     }
10292b360848Smatthias.ringwald }
10302b360848Smatthias.ringwald 
1031b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1032645658c9Smatthias.ringwald 
10339da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1034645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1035645658c9Smatthias.ringwald     if (!service) {
1036645658c9Smatthias.ringwald         // 0x0002 PSM not supported
10372b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1038645658c9Smatthias.ringwald         return;
1039645658c9Smatthias.ringwald     }
1040645658c9Smatthias.ringwald 
10415061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1042645658c9Smatthias.ringwald     if (!hci_connection) {
10432b360848Smatthias.ringwald         //
10449da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1045645658c9Smatthias.ringwald         return;
1046645658c9Smatthias.ringwald     }
10472bd8b7e7S[email protected] 
1048645658c9Smatthias.ringwald     // alloc structure
10499da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1050bb69aaaeS[email protected]     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
10512b360848Smatthias.ringwald     if (!channel){
10522b360848Smatthias.ringwald         // 0x0004 No resources available
10532b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10542b360848Smatthias.ringwald         return;
10552b360848Smatthias.ringwald     }
1056c523d53dS[email protected]     // Init memory (make valgrind happy)
1057c523d53dS[email protected]     memset(channel, 0, sizeof(l2cap_channel_t));
1058645658c9Smatthias.ringwald     // fill in
1059169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
1060169f8b28Smatthias.ringwald     channel->psm = psm;
1061169f8b28Smatthias.ringwald     channel->handle = handle;
1062f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1063b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1064b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1065fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
10660a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1067b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1068df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1069645658c9Smatthias.ringwald 
1070f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
10712985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
10722985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
10739775e25bSmatthias.ringwald     }
10749775e25bSmatthias.ringwald 
1075645658c9Smatthias.ringwald     // set initial state
1076df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1077ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1078e405ae81Smatthias.ringwald 
1079645658c9Smatthias.ringwald     // add to connections list
1080665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1081645658c9Smatthias.ringwald 
1082f85a9399S[email protected]     // assert security requirements
10831eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1084e405ae81Smatthias.ringwald }
1085645658c9Smatthias.ringwald 
1086ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1087e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1088b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1089e405ae81Smatthias.ringwald     if (!channel) {
1090ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1091e405ae81Smatthias.ringwald         return;
1092e405ae81Smatthias.ringwald     }
1093e405ae81Smatthias.ringwald 
1094552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1095e405ae81Smatthias.ringwald 
1096552d92a1Smatthias.ringwald     // process
1097552d92a1Smatthias.ringwald     l2cap_run();
1098e405ae81Smatthias.ringwald }
1099645658c9Smatthias.ringwald 
1100ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){
1101e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1102b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1103e405ae81Smatthias.ringwald     if (!channel) {
1104ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1105e405ae81Smatthias.ringwald         return;
1106e405ae81Smatthias.ringwald     }
1107e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1108e7ff783cSmatthias.ringwald     channel->reason = reason;
1109e7ff783cSmatthias.ringwald     l2cap_run();
1110645658c9Smatthias.ringwald }
1111645658c9Smatthias.ringwald 
11127f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1113b1988dceSmatthias.ringwald 
1114b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1115b1988dceSmatthias.ringwald 
1116f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
111763a7246aSmatthias.ringwald     if (flags & 1) {
111863a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
111963a7246aSmatthias.ringwald     }
112063a7246aSmatthias.ringwald 
11212784b77dSmatthias.ringwald     // accept the other's configuration options
1122f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
11233de7c0caSmatthias.ringwald     uint16_t pos     = 8;
11243de7c0caSmatthias.ringwald     while (pos < end_pos){
112563a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
112663a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
112763a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
112863a7246aSmatthias.ringwald         pos++;
11291dc511deSmatthias.ringwald         uint8_t length = command[pos++];
11301dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
113163a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1132f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
11339da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
113463a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
113563a7246aSmatthias.ringwald         }
11360fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11370fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1138f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
11390fe7a9d0S[email protected]         }
114063a7246aSmatthias.ringwald         // check for unknown options
114163a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1142c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
114363a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11441dc511deSmatthias.ringwald         }
11451dc511deSmatthias.ringwald         pos += length;
11461dc511deSmatthias.ringwald     }
11472784b77dSmatthias.ringwald }
11482784b77dSmatthias.ringwald 
1149fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11509da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
115173cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
115273cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1153019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1154019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1155fa8473a4Smatthias.ringwald     return 1;
1156fa8473a4Smatthias.ringwald }
1157fa8473a4Smatthias.ringwald 
1158fa8473a4Smatthias.ringwald 
11597f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11601e6aba47Smatthias.ringwald 
116100d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
116200d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
116338e5900eSmatthias.ringwald     uint16_t result = 0;
11641e6aba47Smatthias.ringwald 
11659da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1166b35f641cSmatthias.ringwald 
11679a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
11689a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
11699a011532Smatthias.ringwald         switch (channel->state){
1170fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
11719a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
11722b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
11739a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
11749a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
11759a011532Smatthias.ringwald                 break;
11769a011532Smatthias.ringwald 
11779a011532Smatthias.ringwald             default:
11789a011532Smatthias.ringwald                 // ignore in other states
11799a011532Smatthias.ringwald                 break;
11809a011532Smatthias.ringwald         }
11819a011532Smatthias.ringwald         return;
11829a011532Smatthias.ringwald     }
11839a011532Smatthias.ringwald 
118456081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
11851e6aba47Smatthias.ringwald     switch (channel->state) {
11861e6aba47Smatthias.ringwald 
11871e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
11881e6aba47Smatthias.ringwald             switch (code){
11891e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
11905932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1191f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
119238e5900eSmatthias.ringwald                     switch (result) {
119338e5900eSmatthias.ringwald                         case 0:
1194169f8b28Smatthias.ringwald                             // successful connection
1195f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1196fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
119728ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
119838e5900eSmatthias.ringwald                             break;
119938e5900eSmatthias.ringwald                         case 1:
12005932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
12015932bd7cS[email protected]                             l2cap_start_ertx(channel);
120238e5900eSmatthias.ringwald                             break;
120338e5900eSmatthias.ringwald                         default:
1204eb920dbeSmatthias.ringwald                             // channel closed
1205eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1206f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
120738e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1208eb920dbeSmatthias.ringwald 
1209eb920dbeSmatthias.ringwald                             // drop link key if security block
1210eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
12112e77e513S[email protected]                                 hci_drop_link_key_for_bd_addr(channel->address);
1212eb920dbeSmatthias.ringwald                             }
1213eb920dbeSmatthias.ringwald 
1214eb920dbeSmatthias.ringwald                             // discard channel
1215665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1216d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
121738e5900eSmatthias.ringwald                             break;
12181e6aba47Smatthias.ringwald                     }
12191e6aba47Smatthias.ringwald                     break;
122038e5900eSmatthias.ringwald 
122138e5900eSmatthias.ringwald                 default:
12221e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
122338e5900eSmatthias.ringwald                     break;
12241e6aba47Smatthias.ringwald             }
12251e6aba47Smatthias.ringwald             break;
12261e6aba47Smatthias.ringwald 
1227fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1228f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1229ae280e73Smatthias.ringwald             switch (code) {
1230ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
123128ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1232ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
123363a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
123463a7246aSmatthias.ringwald                         // only done if continuation not set
123563a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
123663a7246aSmatthias.ringwald                     }
1237ae280e73Smatthias.ringwald                     break;
12381e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12395932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12405932bd7cS[email protected]                     switch (result){
12415932bd7cS[email protected]                         case 0: // success
12425932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12435932bd7cS[email protected]                             break;
12445932bd7cS[email protected]                         case 4: // pending
12455932bd7cS[email protected]                             l2cap_start_ertx(channel);
12465932bd7cS[email protected]                             break;
12475932bd7cS[email protected]                         default:
1248fe9d8984S[email protected]                             // retry on negative result
1249fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1250fe9d8984S[email protected]                             break;
1251fe9d8984S[email protected]                     }
12525a67bd4aSmatthias.ringwald                     break;
12535a67bd4aSmatthias.ringwald                 default:
12545a67bd4aSmatthias.ringwald                     break;
12551e6aba47Smatthias.ringwald             }
1256fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1257fa8473a4Smatthias.ringwald                 // for open:
12585a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1259fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1260c8e4258aSmatthias.ringwald             }
1261c8e4258aSmatthias.ringwald             break;
1262f62db1e3Smatthias.ringwald 
1263f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1264f62db1e3Smatthias.ringwald             switch (code) {
1265f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
126627a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
126727a923d0Smatthias.ringwald                     break;
12685a67bd4aSmatthias.ringwald                 default:
12695a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
12705a67bd4aSmatthias.ringwald                     break;
127127a923d0Smatthias.ringwald             }
127227a923d0Smatthias.ringwald             break;
127384836b65Smatthias.ringwald 
127484836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
127584836b65Smatthias.ringwald             // @TODO handle incoming requests
127684836b65Smatthias.ringwald             break;
127784836b65Smatthias.ringwald 
127884836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
127984836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
128084836b65Smatthias.ringwald             break;
128110642e45Smatthias.ringwald         default:
128210642e45Smatthias.ringwald             break;
128327a923d0Smatthias.ringwald     }
12849da54300S[email protected]     // log_info("new state %u", channel->state);
128527a923d0Smatthias.ringwald }
128627a923d0Smatthias.ringwald 
128700d93d79Smatthias.ringwald 
12887f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
128900d93d79Smatthias.ringwald 
129000d93d79Smatthias.ringwald     // get code, signalind identifier and command len
129100d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
129200d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
129300d93d79Smatthias.ringwald 
129400d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
129500d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
129663a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
129700d93d79Smatthias.ringwald         return;
129800d93d79Smatthias.ringwald     }
129900d93d79Smatthias.ringwald 
130000d93d79Smatthias.ringwald     // general commands without an assigned channel
130100d93d79Smatthias.ringwald     switch(code) {
130200d93d79Smatthias.ringwald 
130300d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1304f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1305f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
130600d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
13072b83fb7dSmatthias.ringwald             return;
130800d93d79Smatthias.ringwald         }
130900d93d79Smatthias.ringwald 
13102b360848Smatthias.ringwald         case ECHO_REQUEST:
13112b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
13122b83fb7dSmatthias.ringwald             return;
131300d93d79Smatthias.ringwald 
131400d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1315f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
13162b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
13172b83fb7dSmatthias.ringwald             return;
131800d93d79Smatthias.ringwald         }
131900d93d79Smatthias.ringwald 
132000d93d79Smatthias.ringwald         default:
132100d93d79Smatthias.ringwald             break;
132200d93d79Smatthias.ringwald     }
132300d93d79Smatthias.ringwald 
132400d93d79Smatthias.ringwald 
132500d93d79Smatthias.ringwald     // Get potential destination CID
1326f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
132700d93d79Smatthias.ringwald 
132800d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1329665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1330665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1331665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1332665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1333c22aecc9S[email protected]         if (channel->handle != handle) continue;
133400d93d79Smatthias.ringwald         if (code & 1) {
1335b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1336b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
133700d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13384e32727eSmatthias.ringwald                 break;
133900d93d79Smatthias.ringwald             }
134000d93d79Smatthias.ringwald         } else {
1341b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
134200d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
134300d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13444e32727eSmatthias.ringwald                 break;
134500d93d79Smatthias.ringwald             }
134600d93d79Smatthias.ringwald         }
134700d93d79Smatthias.ringwald     }
134800d93d79Smatthias.ringwald }
134900d93d79Smatthias.ringwald 
1350d9a7306aSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ){
135100d93d79Smatthias.ringwald 
135200d93d79Smatthias.ringwald     // Get Channel ID
135300d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
135400d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
135500d93d79Smatthias.ringwald 
13565652b5ffS[email protected]     switch (channel_id) {
13575652b5ffS[email protected] 
13585652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13595652b5ffS[email protected] 
136000d93d79Smatthias.ringwald             uint16_t command_offset = 8;
136100d93d79Smatthias.ringwald             while (command_offset < size) {
136200d93d79Smatthias.ringwald 
136300d93d79Smatthias.ringwald                 // handle signaling commands
136400d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
136500d93d79Smatthias.ringwald 
136600d93d79Smatthias.ringwald                 // increment command_offset
1367f8fbdce0SMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
136800d93d79Smatthias.ringwald             }
13695652b5ffS[email protected]             break;
137000d93d79Smatthias.ringwald         }
137100d93d79Smatthias.ringwald 
13725652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1373*5628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1374*5628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13755652b5ffS[email protected]             }
13765652b5ffS[email protected]             break;
13775652b5ffS[email protected] 
13785652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1379*5628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1380*5628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13815652b5ffS[email protected]             }
13825652b5ffS[email protected]             break;
13835652b5ffS[email protected] 
1384462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1385*5628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1386*5628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1387462e630dS[email protected]             }
1388462e630dS[email protected]             break;
1389462e630dS[email protected] 
13901bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1391ccf076adS[email protected]             switch (packet[8]){
1392ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1393f8fbdce0SMatthias Ringwald                     uint16_t result = little_endian_read_16(packet, 12);
1394ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1395ccf076adS[email protected]                     break;
1396ccf076adS[email protected]                 }
1397ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1398ccf076adS[email protected]                     uint8_t event[10];
1399ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1400ccf076adS[email protected]                     event[1] = 8;
1401ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1402da886c03S[email protected] 
1403da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1404da886c03S[email protected]                     if (connection){
14056d91fb6cSMatthias Ringwald                         if (connection->role != HCI_ROLE_MASTER){
14066d91fb6cSMatthias Ringwald                             // reject command without notifying upper layer when not in master role
14076d91fb6cSMatthias Ringwald                             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14086d91fb6cSMatthias Ringwald                             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14096d91fb6cSMatthias Ringwald                             break;
14106d91fb6cSMatthias Ringwald                         }
1411da886c03S[email protected]                         int update_parameter = 1;
1412a4c06b28SMatthias Ringwald                         le_connection_parameter_range_t existing_range;
1413a4c06b28SMatthias Ringwald                         gap_le_get_connection_parameter_range(existing_range);
1414f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_min = little_endian_read_16(packet,12);
1415f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_max = little_endian_read_16(packet,14);
1416f8fbdce0SMatthias Ringwald                         uint16_t le_conn_latency = little_endian_read_16(packet,16);
1417f8fbdce0SMatthias Ringwald                         uint16_t le_supervision_timeout = little_endian_read_16(packet,18);
1418da886c03S[email protected] 
1419da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1420da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1421da886c03S[email protected] 
1422da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1423da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1424da886c03S[email protected] 
1425da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1426da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1427da886c03S[email protected] 
1428da886c03S[email protected]                         if (update_parameter){
1429da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1430da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1431da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1432da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1433da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1434da886c03S[email protected]                         } else {
1435da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1436da886c03S[email protected]                         }
143779f53f1dSMatthias Ringwald                         connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1];
1438da886c03S[email protected]                     }
1439da886c03S[email protected] 
1440ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
144133c40538SMatthias Ringwald                     if (!l2cap_event_packet_handler) break;
144233c40538SMatthias Ringwald                     (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1443ccf076adS[email protected]                     break;
1444ccf076adS[email protected]                 }
1445ccf076adS[email protected]                 default: {
14461bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14471bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14481bbc0b23S[email protected]                     break;
14491bbc0b23S[email protected]                 }
1450ccf076adS[email protected]             }
1451ccf076adS[email protected]             break;
1452ccf076adS[email protected]         }
14531bbc0b23S[email protected] 
14545652b5ffS[email protected]         default: {
145500d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
145600d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
145700d93d79Smatthias.ringwald             if (channel) {
145817a9b554SMatthias Ringwald                 l2cap_dispatch_to_channel(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
145900d93d79Smatthias.ringwald             }
14605652b5ffS[email protected]             break;
14615652b5ffS[email protected]         }
14625652b5ffS[email protected]     }
146300d93d79Smatthias.ringwald 
14641eb2563eS[email protected]     l2cap_run();
14652718e2e7Smatthias.ringwald }
146600d93d79Smatthias.ringwald 
146715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
146827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1469f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1470f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1471f62db1e3Smatthias.ringwald     // discard channel
14729dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1473665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1474d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1475c8e4258aSmatthias.ringwald }
14761e6aba47Smatthias.ringwald 
14778f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1478665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1479665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1480665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1481665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
14829d9bbc01Smatthias.ringwald         if ( service->psm == psm){
14839d9bbc01Smatthias.ringwald             return service;
14849d9bbc01Smatthias.ringwald         };
14859d9bbc01Smatthias.ringwald     }
14869d9bbc01Smatthias.ringwald     return NULL;
14879d9bbc01Smatthias.ringwald }
14889d9bbc01Smatthias.ringwald 
14897192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
14907192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
14917192e786SMatthias Ringwald }
14927192e786SMatthias Ringwald 
1493e0abb8e7S[email protected] 
1494be2053a6SMatthias 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){
1495be2053a6SMatthias Ringwald 
1496be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1497e0abb8e7S[email protected] 
14984bb582b6Smatthias.ringwald     // check for alread registered psm
14994bb582b6Smatthias.ringwald     // TODO: emit error event
15009d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1501277abc2cSmatthias.ringwald     if (service) {
1502be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1503be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1504277abc2cSmatthias.ringwald     }
15059d9bbc01Smatthias.ringwald 
15064bb582b6Smatthias.ringwald     // alloc structure
15074bb582b6Smatthias.ringwald     // TODO: emit error event
1508bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1509277abc2cSmatthias.ringwald     if (!service) {
1510be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1511be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1512277abc2cSmatthias.ringwald     }
15139d9bbc01Smatthias.ringwald 
15149d9bbc01Smatthias.ringwald     // fill in
15159d9bbc01Smatthias.ringwald     service->psm = psm;
15169d9bbc01Smatthias.ringwald     service->mtu = mtu;
151705ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1518df3354fcS[email protected]     service->required_security_level = security_level;
15199d9bbc01Smatthias.ringwald 
15209d9bbc01Smatthias.ringwald     // add to services list
1521665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1522c0e866bfSmatthias.ringwald 
1523c0e866bfSmatthias.ringwald     // enable page scan
1524c0e866bfSmatthias.ringwald     hci_connectable_control(1);
15255842b6d9Smatthias.ringwald 
1526be2053a6SMatthias Ringwald     return 0;
15279d9bbc01Smatthias.ringwald }
15289d9bbc01Smatthias.ringwald 
152902f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){
1530e0abb8e7S[email protected] 
1531e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1532e0abb8e7S[email protected] 
15339d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1534037d6e48Smatthias.ringwald     if (!service) return;
1535665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1536d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1537c0e866bfSmatthias.ringwald 
1538c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1539665d90f2SMatthias Ringwald     if (!btstack_linked_list_empty(&l2cap_services)) return;
1540c0e866bfSmatthias.ringwald     hci_connectable_control(0);
15419d9bbc01Smatthias.ringwald }
15429d9bbc01Smatthias.ringwald 
15435652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
154405ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
1545*5628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
1546*5628cf69SMatthias Ringwald     if (index < 0) return;
1547*5628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
15485652b5ffS[email protected] }
15495652b5ffS[email protected] 
1550a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
15517192e786SMatthias Ringwald 
15527f02f414SMatthias Ringwald 
15537f02f414SMatthias Ringwald #if 0
15547192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
15557192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
15567192e786SMatthias Ringwald }
15577192e786SMatthias Ringwald /**
15587192e786SMatthias Ringwald  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
15597192e786SMatthias Ringwald  * @param
15607192e786SMatthias Ringwald  */
1561ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm,
15627192e786SMatthias Ringwald     uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){
15637192e786SMatthias Ringwald 
15647192e786SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection);
15657192e786SMatthias Ringwald 
15667192e786SMatthias Ringwald     // check for alread registered psm
15677192e786SMatthias Ringwald     // TODO: emit error event
15687192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
15697192e786SMatthias Ringwald     if (service) {
15707192e786SMatthias Ringwald         log_error("l2cap_le_register_service_internal: PSM %u already registered", psm);
15717192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
15727192e786SMatthias Ringwald         return;
15737192e786SMatthias Ringwald     }
15747192e786SMatthias Ringwald 
15757192e786SMatthias Ringwald     // alloc structure
15767192e786SMatthias Ringwald     // TODO: emit error event
15777192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
15787192e786SMatthias Ringwald     if (!service) {
15797192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
15807192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
15817192e786SMatthias Ringwald         return;
15827192e786SMatthias Ringwald     }
15837192e786SMatthias Ringwald 
15847192e786SMatthias Ringwald     // fill in
15857192e786SMatthias Ringwald     service->psm = psm;
15867192e786SMatthias Ringwald     service->mtu = mtu;
15877192e786SMatthias Ringwald     service->mps = mps;
15887192e786SMatthias Ringwald     service->packet_handler = packet_handler;
15897192e786SMatthias Ringwald     service->required_security_level = security_level;
15907192e786SMatthias Ringwald 
15917192e786SMatthias Ringwald     // add to services list
1592665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
15937192e786SMatthias Ringwald 
15947192e786SMatthias Ringwald     // done
15957192e786SMatthias Ringwald     l2cap_emit_service_registered(connection, 0, psm);
15967192e786SMatthias Ringwald }
15977192e786SMatthias Ringwald 
1598ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) {
15997192e786SMatthias Ringwald 
16007192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
16017192e786SMatthias Ringwald 
16027192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
16037192e786SMatthias Ringwald     if (!service) return;
1604665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
16057192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
16067192e786SMatthias Ringwald }
16077192e786SMatthias Ringwald #endif
16087f02f414SMatthias Ringwald #endif
1609