xref: /btstack/src/l2cap.c (revision 33c40538a628f4eee2a11429611c568f365622fd)
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*33c40538SMatthias Ringwald // prototypes
70*33c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
71*33c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
72*33c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
73*33c40538SMatthias Ringwald static void l2cap_emit_can_send_now(l2cap_channel_t *channel);
74*33c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
75*33c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
76*33c40538SMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
77*33c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
78*33c40538SMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size );
79*33c40538SMatthias Ringwald 
8039bda6d5Smatthias.ringwald 
8139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
822b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
832b83fb7dSmatthias.ringwald static int signaling_responses_pending;
842b83fb7dSmatthias.ringwald 
85*33c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
86*33c40538SMatthias Ringwald 
87fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
88fb37a842SMatthias Ringwald 
898f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
908f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_services;
918f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
928f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
931e6aba47Smatthias.ringwald 
94c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler;
95c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler;
96462e630dS[email protected] static btstack_packet_handler_t connectionless_channel_packet_handler;
97*33c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
9839bda6d5Smatthias.ringwald 
9939bda6d5Smatthias.ringwald 
10071de195eSMatthias Ringwald void l2cap_init(void){
1012b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
102808a48abSmatthias.ringwald 
103f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
104f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1057192e786SMatthias Ringwald     l2cap_le_services = NULL;
1067192e786SMatthias Ringwald     l2cap_le_channels = NULL;
107f5454fc6Smatthias.ringwald 
108*33c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
109c42e2ff2S[email protected]     attribute_protocol_packet_handler = NULL;
110c42e2ff2S[email protected]     security_protocol_packet_handler = NULL;
111462e630dS[email protected]     connectionless_channel_packet_handler = NULL;
112f5454fc6Smatthias.ringwald 
113ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
114ac301f95S[email protected] 
115fcadd0caSmatthias.ringwald     //
1162718e2e7Smatthias.ringwald     // register callback with HCI
117fcadd0caSmatthias.ringwald     //
118d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
119fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
120fb37a842SMatthias Ringwald 
121d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
122fb37a842SMatthias Ringwald 
123c0e866bfSmatthias.ringwald     hci_connectable_control(0); // no services yet
124fcadd0caSmatthias.ringwald }
125fcadd0caSmatthias.ringwald 
126ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
127*33c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1281e6aba47Smatthias.ringwald }
1291e6aba47Smatthias.ringwald 
13017a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
13158de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
13258de5610Smatthias.ringwald }
13358de5610Smatthias.ringwald 
13458de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
135c9dc710bS[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",
136e0abb8e7S[email protected]              status, bd_addr_to_str(channel->address), channel->handle, channel->psm,
137c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
138c9dc710bS[email protected]     uint8_t event[23];
13958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
14058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14158de5610Smatthias.ringwald     event[2] = status;
14258de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
143f8fbdce0SMatthias Ringwald     little_endian_store_16(event,  9, channel->handle);
144f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
145f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
146f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
147f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
148f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
149f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
15058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15117a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
152*33c40538SMatthias Ringwald 
153*33c40538SMatthias Ringwald     // if channel opened successfully, also send can send now if possible
154*33c40538SMatthias Ringwald     if (status) return;
155*33c40538SMatthias Ringwald     if (hci_can_send_acl_packet_now(channel->handle)){
156*33c40538SMatthias Ringwald         l2cap_emit_can_send_now(channel);
157*33c40538SMatthias Ringwald     } else {
158*33c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
159*33c40538SMatthias Ringwald     }
16058de5610Smatthias.ringwald }
16158de5610Smatthias.ringwald 
16258de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
163e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
16458de5610Smatthias.ringwald     uint8_t event[4];
16558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
16658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
167f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
16858de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
16917a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
17058de5610Smatthias.ringwald }
17158de5610Smatthias.ringwald 
17258de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
173e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
174e0abb8e7S[email protected]              bd_addr_to_str(channel->address), channel->handle,  channel->psm, channel->local_cid, channel->remote_cid);
17558de5610Smatthias.ringwald     uint8_t event[16];
17658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
17758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
17858de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
179f8fbdce0SMatthias Ringwald     little_endian_store_16(event,  8, channel->handle);
180f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
181f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
182f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
18358de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18417a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
1850af41d30Smatthias.ringwald }
186808a48abSmatthias.ringwald 
187*33c40538SMatthias Ringwald static void l2cap_emit_can_send_now(l2cap_channel_t *channel) {
188*33c40538SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel->local_cid);
189*33c40538SMatthias Ringwald     uint8_t event[4];
190*33c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
191*33c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
192*33c40538SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
193*33c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
194*33c40538SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
195*33c40538SMatthias Ringwald }
196*33c40538SMatthias Ringwald 
1977f02f414SMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){
198ccf076adS[email protected]     uint8_t event[6];
199ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
200ccf076adS[email protected]     event[1] = 4;
201f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, handle);
202f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
203ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
204*33c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
205*33c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
206ccf076adS[email protected] }
207ccf076adS[email protected] 
2087f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
209665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
210665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
211665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
212665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
213b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
214f62db1e3Smatthias.ringwald             return channel;
215f62db1e3Smatthias.ringwald         }
216f62db1e3Smatthias.ringwald     }
217f62db1e3Smatthias.ringwald     return NULL;
218f62db1e3Smatthias.ringwald }
219f62db1e3Smatthias.ringwald 
2206b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2216b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2226b1fde37Smatthias.ringwald     if (!channel) return 0;
223*33c40538SMatthias Ringwald     int can_send = hci_can_send_acl_packet_now(channel->handle);
224*33c40538SMatthias Ringwald     if (!can_send){
225*33c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
226*33c40538SMatthias Ringwald     }
227*33c40538SMatthias Ringwald     return can_send;
2287856fb31S[email protected] }
2297856fb31S[email protected] 
23083e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
23183e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
23283e7cdd9SMatthias Ringwald     if (!channel) return 0;
233*33c40538SMatthias Ringwald     int can_send = hci_can_send_prepared_acl_packet_now(channel->handle);
234*33c40538SMatthias Ringwald     if (!can_send){
235*33c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 1;
236*33c40538SMatthias Ringwald     }
237*33c40538SMatthias Ringwald     return can_send;
23883e7cdd9SMatthias Ringwald }
23983e7cdd9SMatthias Ringwald 
2406cd4da6bS[email protected] int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle){
2416cd4da6bS[email protected]     return hci_can_send_acl_packet_now(handle);
2423cab4fcaS[email protected] }
2433cab4fcaS[email protected] 
24496cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
24596cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
24696cbd662Smatthias.ringwald     if (channel) {
24796cbd662Smatthias.ringwald         return channel->remote_mtu;
24896cbd662Smatthias.ringwald     }
24996cbd662Smatthias.ringwald     return 0;
25096cbd662Smatthias.ringwald }
25196cbd662Smatthias.ringwald 
252ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
253665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
254665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
255665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
256665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2575932bd7cS[email protected]         if ( &channel->rtx == ts) {
2585932bd7cS[email protected]             return channel;
2595932bd7cS[email protected]         }
2605932bd7cS[email protected]     }
2615932bd7cS[email protected]     return NULL;
2625932bd7cS[email protected] }
2635932bd7cS[email protected] 
264ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
2655932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2665932bd7cS[email protected]     if (!ts) return;
2675932bd7cS[email protected] 
2685932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2695932bd7cS[email protected] 
2705932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
2715932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
2725932bd7cS[email protected]     // notify client
2735932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
2745932bd7cS[email protected] 
2755932bd7cS[email protected]     // discard channel
2769dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
277665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
2785932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
2795932bd7cS[email protected] }
2805932bd7cS[email protected] 
2815932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
2825932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
283528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
2845932bd7cS[email protected] }
2855932bd7cS[email protected] 
2865932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
2875932bd7cS[email protected]     l2cap_stop_rtx(channel);
288cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
289528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
290528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
291528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
2925932bd7cS[email protected] }
2935932bd7cS[email protected] 
2945932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
2955932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
2965932bd7cS[email protected]     l2cap_stop_rtx(channel);
297528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
298528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
299528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3005932bd7cS[email protected] }
3015932bd7cS[email protected] 
30271de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
303ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
304ac301f95S[email protected] }
305ac301f95S[email protected] 
306df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
307ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
308df3354fcS[email protected] }
3095932bd7cS[email protected] 
3107f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
311b1d43497Smatthias.ringwald 
312a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3139da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
314b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
315b1d43497Smatthias.ringwald     }
316b1d43497Smatthias.ringwald 
3179da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3182a373862S[email protected]     hci_reserve_packet_buffer();
319facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
32058de5610Smatthias.ringwald     va_list argptr;
32158de5610Smatthias.ringwald     va_start(argptr, identifier);
32270efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
32358de5610Smatthias.ringwald     va_end(argptr);
3249da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
325826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
32658de5610Smatthias.ringwald }
32758de5610Smatthias.ringwald 
328a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3297f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
33070efece1S[email protected] 
331a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3329da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
33370efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
33470efece1S[email protected]     }
33570efece1S[email protected] 
3369da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3372a373862S[email protected]     hci_reserve_packet_buffer();
338facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
33970efece1S[email protected]     va_list argptr;
34070efece1S[email protected]     va_start(argptr, identifier);
34170efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
34270efece1S[email protected]     va_end(argptr);
3439da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
344826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
34570efece1S[email protected] }
34670efece1S[email protected] #endif
34770efece1S[email protected] 
348b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
349facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
350b1d43497Smatthias.ringwald }
3516218e6f1Smatthias.ringwald 
3526b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3536b4af23dS[email protected]     return hci_reserve_packet_buffer();
3546b4af23dS[email protected] }
3556b4af23dS[email protected] 
35668a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
35768a0fcf7S[email protected]     hci_release_packet_buffer();
35868a0fcf7S[email protected] }
35968a0fcf7S[email protected] 
36068a0fcf7S[email protected] 
361b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
362b1d43497Smatthias.ringwald 
363c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
364c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
365c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
366c8b9416aS[email protected]     }
367c8b9416aS[email protected] 
36858de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
369b1d43497Smatthias.ringwald     if (!channel) {
3709da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
371b1d43497Smatthias.ringwald         return -1;   // TODO: define error
3726218e6f1Smatthias.ringwald     }
3736218e6f1Smatthias.ringwald 
374a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(channel->handle)){
3759da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
376a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
377a35252c8S[email protected]     }
378a35252c8S[email protected] 
3793aff022fSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle);
380b1d43497Smatthias.ringwald 
381facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
382b1d43497Smatthias.ringwald 
383e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
384e9772277S[email protected] 
385e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
386f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14));
38758de5610Smatthias.ringwald     // 2 - ACL length
388f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
38958de5610Smatthias.ringwald     // 4 - L2CAP packet length
390f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
39158de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
392f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
39358de5610Smatthias.ringwald     // send
394826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
39591b99603Smatthias.ringwald 
3966218e6f1Smatthias.ringwald     return err;
39758de5610Smatthias.ringwald }
39858de5610Smatthias.ringwald 
3992149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
4002149f12eSmatthias.ringwald 
401c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
402c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4032149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4042149f12eSmatthias.ringwald     }
4052149f12eSmatthias.ringwald 
406a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(handle)){
4079da54300S[email protected]         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid);
408c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
409c8b9416aS[email protected]     }
410c8b9416aS[email protected] 
4119da54300S[email protected]     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid);
4122149f12eSmatthias.ringwald 
413facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4142149f12eSmatthias.ringwald 
415e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
416e9772277S[email protected] 
417e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
418f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
4192149f12eSmatthias.ringwald     // 2 - ACL length
420f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4212149f12eSmatthias.ringwald     // 4 - L2CAP packet length
422f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4232149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
424f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4252149f12eSmatthias.ringwald     // send
426826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4272149f12eSmatthias.ringwald 
4282149f12eSmatthias.ringwald     return err;
4292149f12eSmatthias.ringwald }
4302149f12eSmatthias.ringwald 
431ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
432b1d43497Smatthias.ringwald 
433a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
434a35252c8S[email protected]     if (!channel) {
435ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
436a35252c8S[email protected]         return -1;   // TODO: define error
437a35252c8S[email protected]     }
438a35252c8S[email protected] 
439f0efaa57S[email protected]     if (len > channel->remote_mtu){
440ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
441f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
442f0efaa57S[email protected]     }
443f0efaa57S[email protected] 
444a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(channel->handle)){
445ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
446b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
447b1d43497Smatthias.ringwald     }
448b1d43497Smatthias.ringwald 
4492a373862S[email protected]     hci_reserve_packet_buffer();
450facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
451b1d43497Smatthias.ringwald 
452b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
453b1d43497Smatthias.ringwald 
454b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
455b1d43497Smatthias.ringwald }
456b1d43497Smatthias.ringwald 
4572149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
4582149f12eSmatthias.ringwald 
459a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
460ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
4612149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4622149f12eSmatthias.ringwald     }
4632149f12eSmatthias.ringwald 
4642a373862S[email protected]     hci_reserve_packet_buffer();
465facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4662149f12eSmatthias.ringwald 
4672149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4682149f12eSmatthias.ringwald 
4692149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4702149f12eSmatthias.ringwald }
4712149f12eSmatthias.ringwald 
4720e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){
4730e37e417S[email protected]     return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data);
4740e37e417S[email protected] }
4750e37e417S[email protected] 
47628ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
47728ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
47828ca2b46S[email protected] }
47928ca2b46S[email protected] 
48028ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
48128ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
48228ca2b46S[email protected] }
48328ca2b46S[email protected] 
48428ca2b46S[email protected] 
485b1d43497Smatthias.ringwald 
4868158c421Smatthias.ringwald // MARK: L2CAP_RUN
4872cd0be45Smatthias.ringwald // process outstanding signaling tasks
4887f02f414SMatthias Ringwald static void l2cap_run(void){
4892b83fb7dSmatthias.ringwald 
49022c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
49122c29ab4SMatthias Ringwald 
4922b83fb7dSmatthias.ringwald     // check pending signaling responses
4932b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
4942b83fb7dSmatthias.ringwald 
4952b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
496a35252c8S[email protected] 
497a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
498a35252c8S[email protected] 
4992b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5002b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
50163a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
502f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5032b83fb7dSmatthias.ringwald 
504f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
505f53da564S[email protected]         signaling_responses_pending--;
506f53da564S[email protected]         int i;
507f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
508f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
509f53da564S[email protected]         }
510f53da564S[email protected] 
511f53da564S[email protected]         switch (response_code){
5122b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5132b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5142bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5154d816277S[email protected]                 if (result == 0x0003){
5162bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5174d816277S[email protected]                 }
5182b360848Smatthias.ringwald                 break;
5192b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5202b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5212b83fb7dSmatthias.ringwald                 break;
5222b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5233b0484b3S[email protected]                 switch (infoType){
5243b0484b3S[email protected]                     case 1: { // Connectionless MTU
5253b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5263b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5273b0484b3S[email protected]                         break;
5283b0484b3S[email protected]                     }
5293b0484b3S[email protected]                     case 2: { // Extended Features Supported
530462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
531462e630dS[email protected]                         uint32_t features = 0x280;
5323b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5333b0484b3S[email protected]                         break;
5343b0484b3S[email protected]                     }
5353b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5363b0484b3S[email protected]                         uint8_t map[8];
5373b0484b3S[email protected]                         memset(map, 0, 8);
538462e630dS[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02)
5393b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5403b0484b3S[email protected]                         break;
5413b0484b3S[email protected]                     }
5423b0484b3S[email protected]                     default:
5432b83fb7dSmatthias.ringwald                         // all other types are not supported
5442b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5453b0484b3S[email protected]                         break;
5462b83fb7dSmatthias.ringwald                 }
5472b83fb7dSmatthias.ringwald                 break;
54863a7246aSmatthias.ringwald             case COMMAND_REJECT:
5495ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
550a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
55170efece1S[email protected]             case COMMAND_REJECT_LE:
55270efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
55363a7246aSmatthias.ringwald                 break;
55470efece1S[email protected] #endif
5552b83fb7dSmatthias.ringwald             default:
5562b83fb7dSmatthias.ringwald                 // should not happen
5572b83fb7dSmatthias.ringwald                 break;
5582b83fb7dSmatthias.ringwald         }
5592b83fb7dSmatthias.ringwald     }
5602b83fb7dSmatthias.ringwald 
561ae280e73Smatthias.ringwald     uint8_t  config_options[4];
562665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
563665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
564665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
565baf94f06S[email protected] 
566665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
56722c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
5682cd0be45Smatthias.ringwald         switch (channel->state){
5692cd0be45Smatthias.ringwald 
570df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
571ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
572baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
573a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
574ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
575a00031e2S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
576ad671560S[email protected]                 }
577ad671560S[email protected]                 break;
578ad671560S[email protected] 
57902b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
580baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
58164472d52Smatthias.ringwald                 // send connection request - set state first
58264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
58302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
5848f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
58502b22dc4Smatthias.ringwald                 break;
58602b22dc4Smatthias.ringwald 
587e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
588baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
58922c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
5901eb2563eS[email protected]                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
591e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
5929dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
593665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
594d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
595e7ff783cSmatthias.ringwald                 break;
596e7ff783cSmatthias.ringwald 
597552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
598baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
599fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
60028ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
6012a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
602552d92a1Smatthias.ringwald                 break;
603552d92a1Smatthias.ringwald 
6046fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
605baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
6066fdcc387Smatthias.ringwald                 // success, start l2cap handshake
607b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6086fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
6092a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6105932bd7cS[email protected]                 l2cap_start_rtx(channel);
6116fdcc387Smatthias.ringwald                 break;
6126fdcc387Smatthias.ringwald 
613fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
614baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
61573cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
61663a7246aSmatthias.ringwald                     uint16_t flags = 0;
61728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
61863a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
61963a7246aSmatthias.ringwald                         flags = 1;
62063a7246aSmatthias.ringwald                     } else {
62128ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
62263a7246aSmatthias.ringwald                     }
62363a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
62463a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
62563a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
62663a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
62763a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
628f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
62963a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
63063a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
63163a7246aSmatthias.ringwald                     } else {
63263a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
63363a7246aSmatthias.ringwald                     }
63463a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
635fa8473a4Smatthias.ringwald                 }
63673cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
63728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
63828ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
639b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
640ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
641ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
642f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
643b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6445932bd7cS[email protected]                     l2cap_start_rtx(channel);
645fa8473a4Smatthias.ringwald                 }
646fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
647552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
648552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
649fa8473a4Smatthias.ringwald                 }
650552d92a1Smatthias.ringwald                 break;
651552d92a1Smatthias.ringwald 
652e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
653baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
65422c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
655b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6565932bd7cS[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 :)
657756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
658e7ff783cSmatthias.ringwald                 break;
659e7ff783cSmatthias.ringwald 
660e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
661baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
662b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6632cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
6642a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
6652cd0be45Smatthias.ringwald                 break;
6662cd0be45Smatthias.ringwald             default:
6672cd0be45Smatthias.ringwald                 break;
6682cd0be45Smatthias.ringwald         }
6692cd0be45Smatthias.ringwald     }
670da886c03S[email protected] 
671a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
672da886c03S[email protected]     // send l2cap con paramter update if necessary
673da886c03S[email protected]     hci_connections_get_iterator(&it);
674665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
675665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
6765d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
677b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
678da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
679b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
680b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
681b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
682b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
683b68d7bc3SMatthias Ringwald                 break;
684da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
685b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
686b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
687da886c03S[email protected]                 break;
688da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
689b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
690b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
691da886c03S[email protected]                 break;
692da886c03S[email protected]             default:
693da886c03S[email protected]                 break;
694da886c03S[email protected]         }
695da886c03S[email protected]     }
6964d7157c3S[email protected] #endif
697da886c03S[email protected] 
69822c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
6992cd0be45Smatthias.ringwald }
7002cd0be45Smatthias.ringwald 
7014aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7024ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
703fa8c92f6Smatthias.ringwald }
704fa8c92f6Smatthias.ringwald 
70571de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
7064ff786cfS[email protected]     return l2cap_max_mtu();
707e5e1518dS[email protected] }
708e5e1518dS[email protected] 
7092df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){
7102df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7115533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7122df5dadcS[email protected]         // success, start l2cap handshake
7132df5dadcS[email protected]         channel->handle = handle;
7142df5dadcS[email protected]         // check remote SSP feature first
7152df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7162df5dadcS[email protected]     }
7172df5dadcS[email protected] }
7182df5dadcS[email protected] 
7192df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7202df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7212df5dadcS[email protected] 
7222df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
723ac301f95S[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));
7242df5dadcS[email protected]     if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7252df5dadcS[email protected]         // request security level 2
7262df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
7271429b2d6S[email protected]         gap_request_security_level(channel->handle, LEVEL_2);
7282df5dadcS[email protected]         return;
7292df5dadcS[email protected]     }
7302df5dadcS[email protected]     // fine, go ahead
7312df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7322df5dadcS[email protected] }
7332df5dadcS[email protected] 
7349077cb15SMatthias Ringwald /**
7359077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
7369077cb15SMatthias Ringwald  * @param packet_handler
7379077cb15SMatthias Ringwald  * @param address
7389077cb15SMatthias Ringwald  * @param psm
7399077cb15SMatthias Ringwald  * @param mtu
7409077cb15SMatthias Ringwald  * @param local_cid
7419077cb15SMatthias Ringwald  */
7429077cb15SMatthias 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){
7439077cb15SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
7449077cb15SMatthias Ringwald 
7459077cb15SMatthias Ringwald     // alloc structure
7469077cb15SMatthias Ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
7479077cb15SMatthias Ringwald     if (!chan) {
7489077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
7499077cb15SMatthias Ringwald     }
7509077cb15SMatthias Ringwald 
7519077cb15SMatthias Ringwald      // Init memory (make valgrind happy)
7529077cb15SMatthias Ringwald     memset(chan, 0, sizeof(l2cap_channel_t));
7539077cb15SMatthias Ringwald     // limit local mtu to max acl packet length - l2cap header
7549077cb15SMatthias Ringwald     if (mtu > l2cap_max_mtu()) {
7559077cb15SMatthias Ringwald         mtu = l2cap_max_mtu();
7569077cb15SMatthias Ringwald     }
7579077cb15SMatthias Ringwald 
7589077cb15SMatthias Ringwald     // fill in
7599077cb15SMatthias Ringwald     BD_ADDR_COPY(chan->address, address);
7609077cb15SMatthias Ringwald     chan->psm = psm;
7619077cb15SMatthias Ringwald     chan->handle = 0;
7629077cb15SMatthias Ringwald     chan->packet_handler = channel_packet_handler;
7639077cb15SMatthias Ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
7649077cb15SMatthias Ringwald     chan->local_mtu = mtu;
7659077cb15SMatthias Ringwald     chan->local_cid = l2cap_next_local_cid();
7669077cb15SMatthias Ringwald 
7679077cb15SMatthias Ringwald     // set initial state
7689077cb15SMatthias Ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
7699077cb15SMatthias Ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
7709077cb15SMatthias Ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
7719077cb15SMatthias Ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
7729077cb15SMatthias Ringwald     chan->required_security_level = LEVEL_0;
7739077cb15SMatthias Ringwald 
7749077cb15SMatthias Ringwald     // add to connections list
775665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan);
7769077cb15SMatthias Ringwald 
7779077cb15SMatthias Ringwald     // store local_cid
7789077cb15SMatthias Ringwald     if (out_local_cid){
7799077cb15SMatthias Ringwald        *out_local_cid = chan->local_cid;
7809077cb15SMatthias Ringwald     }
7819077cb15SMatthias Ringwald 
7829077cb15SMatthias Ringwald     // check if hci connection is already usable
7839077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
7849077cb15SMatthias Ringwald     if (conn){
785add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
7869077cb15SMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, chan);
7879077cb15SMatthias Ringwald         // check if remote supported fearures are already received
7889077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
7899077cb15SMatthias Ringwald             l2cap_handle_remote_supported_features_received(chan);
7909077cb15SMatthias Ringwald         }
7919077cb15SMatthias Ringwald     }
7929077cb15SMatthias Ringwald 
7939077cb15SMatthias Ringwald     l2cap_run();
7949077cb15SMatthias Ringwald 
7959077cb15SMatthias Ringwald     return 0;
7969077cb15SMatthias Ringwald }
7979077cb15SMatthias Ringwald 
798ce8f182eSMatthias Ringwald void
799ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
800e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
801b35f641cSmatthias.ringwald     // find channel for local_cid
802b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
803f62db1e3Smatthias.ringwald     if (channel) {
804e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
805f62db1e3Smatthias.ringwald     }
8062cd0be45Smatthias.ringwald     // process
8072cd0be45Smatthias.ringwald     l2cap_run();
80843625864Smatthias.ringwald }
8091e6aba47Smatthias.ringwald 
810afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
811665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
812665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
813665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
814665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
815c22aecc9S[email protected]         if ( BD_ADDR_CMP( channel->address, address) != 0) continue;
816c22aecc9S[email protected]         // channel for this address found
817c22aecc9S[email protected]         switch (channel->state){
818c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
819c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
820afde0c52Smatthias.ringwald                 // failure, forward error code
821afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
822afde0c52Smatthias.ringwald                 // discard channel
8239dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
824665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
825d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
826c22aecc9S[email protected]                 break;
827c22aecc9S[email protected]             default:
828c22aecc9S[email protected]                 break;
829afde0c52Smatthias.ringwald         }
830afde0c52Smatthias.ringwald     }
831afde0c52Smatthias.ringwald }
832afde0c52Smatthias.ringwald 
833afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
834665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
835665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
836665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
837665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
838afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
8392df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
840afde0c52Smatthias.ringwald         }
841afde0c52Smatthias.ringwald     }
8426fdcc387Smatthias.ringwald     // process
8436fdcc387Smatthias.ringwald     l2cap_run();
844afde0c52Smatthias.ringwald }
845b448a0e7Smatthias.ringwald 
846*33c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
847*33c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
848*33c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
849*33c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
850*33c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
851*33c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
852*33c40538SMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->handle)) continue;
853*33c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
854*33c40538SMatthias Ringwald         l2cap_emit_can_send_now(channel);
855*33c40538SMatthias Ringwald     }
856*33c40538SMatthias Ringwald }
857*33c40538SMatthias Ringwald 
858d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
859afde0c52Smatthias.ringwald 
860afde0c52Smatthias.ringwald     bd_addr_t address;
861afde0c52Smatthias.ringwald     hci_con_handle_t handle;
862665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
8632d00edd4Smatthias.ringwald     int hci_con_used;
864afde0c52Smatthias.ringwald 
865afde0c52Smatthias.ringwald     switch(packet[0]){
866afde0c52Smatthias.ringwald 
867afde0c52Smatthias.ringwald         // handle connection complete events
868afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
869afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
870afde0c52Smatthias.ringwald             if (packet[2] == 0){
871f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
872afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
873afde0c52Smatthias.ringwald             } else {
874afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
875afde0c52Smatthias.ringwald             }
876afde0c52Smatthias.ringwald             break;
877afde0c52Smatthias.ringwald 
878afde0c52Smatthias.ringwald         // handle successful create connection cancel command
879afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
880afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
881afde0c52Smatthias.ringwald                 if (packet[5] == 0){
882afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
883afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
884afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
88503cfbabcSmatthias.ringwald                 }
8861e6aba47Smatthias.ringwald             }
88739d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
88839d59809Smatthias.ringwald             break;
88939d59809Smatthias.ringwald 
89039d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
89139d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
892afde0c52Smatthias.ringwald             break;
89327a923d0Smatthias.ringwald 
8941e6aba47Smatthias.ringwald         // handle disconnection complete events
895afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
896c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
897f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
898665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
899665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
900665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
901c22aecc9S[email protected]                 if (channel->handle != handle) continue;
90215ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
9039dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
904665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
905d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
90627a923d0Smatthias.ringwald             }
907afde0c52Smatthias.ringwald             break;
908fcadd0caSmatthias.ringwald 
909*33c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
910*33c40538SMatthias Ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
9116218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
91202b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
913*33c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
9146218e6f1Smatthias.ringwald             break;
9156218e6f1Smatthias.ringwald 
916ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
917afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
918f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
919bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
92080ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
9212d00edd4Smatthias.ringwald             hci_con_used = 0;
922665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
923665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
924665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
925c22aecc9S[email protected]                 if (channel->handle != handle) continue;
9262d00edd4Smatthias.ringwald                 hci_con_used = 1;
927c22aecc9S[email protected]                 break;
928ee091cf1Smatthias.ringwald             }
9292d00edd4Smatthias.ringwald             if (hci_con_used) break;
930d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
9319edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
932afde0c52Smatthias.ringwald             break;
933ee091cf1Smatthias.ringwald 
934df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
935f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
936665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
937665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
938665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
939df3354fcS[email protected]                 if (channel->handle != handle) continue;
9402df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
941df3354fcS[email protected]                 break;
942df3354fcS[email protected]             }
943c22aecc9S[email protected]             break;
944df3354fcS[email protected] 
9455611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
946f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
947bd63148eS[email protected]             log_info("l2cap - security level update");
948665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
949665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
950665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
951f85a9399S[email protected]                 if (channel->handle != handle) continue;
9525533f01eS[email protected] 
953bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
954bd63148eS[email protected] 
955e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
9565533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
9575533f01eS[email protected] 
958df3354fcS[email protected]                 switch (channel->state){
959df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
9605533f01eS[email protected]                         if (actual_level >= required_level){
961f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
962f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
9631eb2563eS[email protected]                         } else {
964775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
9651eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
9661eb2563eS[email protected]                         }
967df3354fcS[email protected]                         break;
968df3354fcS[email protected] 
969df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
9705533f01eS[email protected]                         if (actual_level >= required_level){
971df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
972df3354fcS[email protected]                         } else {
973df3354fcS[email protected]                             // disconnnect, authentication not good enough
974df3354fcS[email protected]                             hci_disconnect_security_block(handle);
975df3354fcS[email protected]                         }
976df3354fcS[email protected]                         break;
977df3354fcS[email protected] 
978df3354fcS[email protected]                     default:
979df3354fcS[email protected]                         break;
980df3354fcS[email protected]                 }
981f85a9399S[email protected]             }
982f85a9399S[email protected]             break;
983f85a9399S[email protected] 
984afde0c52Smatthias.ringwald         default:
985afde0c52Smatthias.ringwald             break;
986afde0c52Smatthias.ringwald     }
987afde0c52Smatthias.ringwald 
988bd63148eS[email protected]     l2cap_run();
9891e6aba47Smatthias.ringwald }
9901e6aba47Smatthias.ringwald 
991afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
992b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
993e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
994e7ff783cSmatthias.ringwald     l2cap_run();
99584836b65Smatthias.ringwald }
99684836b65Smatthias.ringwald 
9972b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
9984cf56b4aSmatthias.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."
9992b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
10002b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
10012b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
10022b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
10032b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
10042b360848Smatthias.ringwald         signaling_responses_pending++;
10052b360848Smatthias.ringwald         l2cap_run();
10062b360848Smatthias.ringwald     }
10072b360848Smatthias.ringwald }
10082b360848Smatthias.ringwald 
1009b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1010645658c9Smatthias.ringwald 
10119da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1012645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1013645658c9Smatthias.ringwald     if (!service) {
1014645658c9Smatthias.ringwald         // 0x0002 PSM not supported
10152b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1016645658c9Smatthias.ringwald         return;
1017645658c9Smatthias.ringwald     }
1018645658c9Smatthias.ringwald 
10195061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1020645658c9Smatthias.ringwald     if (!hci_connection) {
10212b360848Smatthias.ringwald         //
10229da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1023645658c9Smatthias.ringwald         return;
1024645658c9Smatthias.ringwald     }
10252bd8b7e7S[email protected] 
1026645658c9Smatthias.ringwald     // alloc structure
10279da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1028bb69aaaeS[email protected]     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
10292b360848Smatthias.ringwald     if (!channel){
10302b360848Smatthias.ringwald         // 0x0004 No resources available
10312b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10322b360848Smatthias.ringwald         return;
10332b360848Smatthias.ringwald     }
1034c523d53dS[email protected]     // Init memory (make valgrind happy)
1035c523d53dS[email protected]     memset(channel, 0, sizeof(l2cap_channel_t));
1036645658c9Smatthias.ringwald     // fill in
1037169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
1038169f8b28Smatthias.ringwald     channel->psm = psm;
1039169f8b28Smatthias.ringwald     channel->handle = handle;
1040f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1041b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1042b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1043fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
10440a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1045b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1046df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1047645658c9Smatthias.ringwald 
1048f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
10492985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
10502985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
10519775e25bSmatthias.ringwald     }
10529775e25bSmatthias.ringwald 
1053645658c9Smatthias.ringwald     // set initial state
1054df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1055ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1056e405ae81Smatthias.ringwald 
1057645658c9Smatthias.ringwald     // add to connections list
1058665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1059645658c9Smatthias.ringwald 
1060f85a9399S[email protected]     // assert security requirements
10611eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1062e405ae81Smatthias.ringwald }
1063645658c9Smatthias.ringwald 
1064ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1065e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1066b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1067e405ae81Smatthias.ringwald     if (!channel) {
1068ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1069e405ae81Smatthias.ringwald         return;
1070e405ae81Smatthias.ringwald     }
1071e405ae81Smatthias.ringwald 
1072552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1073e405ae81Smatthias.ringwald 
1074552d92a1Smatthias.ringwald     // process
1075552d92a1Smatthias.ringwald     l2cap_run();
1076e405ae81Smatthias.ringwald }
1077645658c9Smatthias.ringwald 
1078ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){
1079e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1080b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1081e405ae81Smatthias.ringwald     if (!channel) {
1082ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1083e405ae81Smatthias.ringwald         return;
1084e405ae81Smatthias.ringwald     }
1085e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1086e7ff783cSmatthias.ringwald     channel->reason = reason;
1087e7ff783cSmatthias.ringwald     l2cap_run();
1088645658c9Smatthias.ringwald }
1089645658c9Smatthias.ringwald 
10907f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1091b1988dceSmatthias.ringwald 
1092b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1093b1988dceSmatthias.ringwald 
1094f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
109563a7246aSmatthias.ringwald     if (flags & 1) {
109663a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
109763a7246aSmatthias.ringwald     }
109863a7246aSmatthias.ringwald 
10992784b77dSmatthias.ringwald     // accept the other's configuration options
1100f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
11013de7c0caSmatthias.ringwald     uint16_t pos     = 8;
11023de7c0caSmatthias.ringwald     while (pos < end_pos){
110363a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
110463a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
110563a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
110663a7246aSmatthias.ringwald         pos++;
11071dc511deSmatthias.ringwald         uint8_t length = command[pos++];
11081dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
110963a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1110f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
11119da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
111263a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
111363a7246aSmatthias.ringwald         }
11140fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11150fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1116f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
11170fe7a9d0S[email protected]         }
111863a7246aSmatthias.ringwald         // check for unknown options
111963a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1120c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
112163a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11221dc511deSmatthias.ringwald         }
11231dc511deSmatthias.ringwald         pos += length;
11241dc511deSmatthias.ringwald     }
11252784b77dSmatthias.ringwald }
11262784b77dSmatthias.ringwald 
1127fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11289da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
112973cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
113073cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1131019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1132019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1133fa8473a4Smatthias.ringwald     return 1;
1134fa8473a4Smatthias.ringwald }
1135fa8473a4Smatthias.ringwald 
1136fa8473a4Smatthias.ringwald 
11377f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11381e6aba47Smatthias.ringwald 
113900d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
114000d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
114138e5900eSmatthias.ringwald     uint16_t result = 0;
11421e6aba47Smatthias.ringwald 
11439da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1144b35f641cSmatthias.ringwald 
11459a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
11469a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
11479a011532Smatthias.ringwald         switch (channel->state){
1148fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
11499a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
11502b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
11519a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
11529a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
11539a011532Smatthias.ringwald                 break;
11549a011532Smatthias.ringwald 
11559a011532Smatthias.ringwald             default:
11569a011532Smatthias.ringwald                 // ignore in other states
11579a011532Smatthias.ringwald                 break;
11589a011532Smatthias.ringwald         }
11599a011532Smatthias.ringwald         return;
11609a011532Smatthias.ringwald     }
11619a011532Smatthias.ringwald 
116256081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
11631e6aba47Smatthias.ringwald     switch (channel->state) {
11641e6aba47Smatthias.ringwald 
11651e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
11661e6aba47Smatthias.ringwald             switch (code){
11671e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
11685932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1169f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
117038e5900eSmatthias.ringwald                     switch (result) {
117138e5900eSmatthias.ringwald                         case 0:
1172169f8b28Smatthias.ringwald                             // successful connection
1173f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1174fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
117528ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
117638e5900eSmatthias.ringwald                             break;
117738e5900eSmatthias.ringwald                         case 1:
11785932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
11795932bd7cS[email protected]                             l2cap_start_ertx(channel);
118038e5900eSmatthias.ringwald                             break;
118138e5900eSmatthias.ringwald                         default:
1182eb920dbeSmatthias.ringwald                             // channel closed
1183eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1184f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
118538e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1186eb920dbeSmatthias.ringwald 
1187eb920dbeSmatthias.ringwald                             // drop link key if security block
1188eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
11892e77e513S[email protected]                                 hci_drop_link_key_for_bd_addr(channel->address);
1190eb920dbeSmatthias.ringwald                             }
1191eb920dbeSmatthias.ringwald 
1192eb920dbeSmatthias.ringwald                             // discard channel
1193665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1194d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
119538e5900eSmatthias.ringwald                             break;
11961e6aba47Smatthias.ringwald                     }
11971e6aba47Smatthias.ringwald                     break;
119838e5900eSmatthias.ringwald 
119938e5900eSmatthias.ringwald                 default:
12001e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
120138e5900eSmatthias.ringwald                     break;
12021e6aba47Smatthias.ringwald             }
12031e6aba47Smatthias.ringwald             break;
12041e6aba47Smatthias.ringwald 
1205fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1206f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1207ae280e73Smatthias.ringwald             switch (code) {
1208ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
120928ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1210ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
121163a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
121263a7246aSmatthias.ringwald                         // only done if continuation not set
121363a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
121463a7246aSmatthias.ringwald                     }
1215ae280e73Smatthias.ringwald                     break;
12161e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12175932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12185932bd7cS[email protected]                     switch (result){
12195932bd7cS[email protected]                         case 0: // success
12205932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12215932bd7cS[email protected]                             break;
12225932bd7cS[email protected]                         case 4: // pending
12235932bd7cS[email protected]                             l2cap_start_ertx(channel);
12245932bd7cS[email protected]                             break;
12255932bd7cS[email protected]                         default:
1226fe9d8984S[email protected]                             // retry on negative result
1227fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1228fe9d8984S[email protected]                             break;
1229fe9d8984S[email protected]                     }
12305a67bd4aSmatthias.ringwald                     break;
12315a67bd4aSmatthias.ringwald                 default:
12325a67bd4aSmatthias.ringwald                     break;
12331e6aba47Smatthias.ringwald             }
1234fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1235fa8473a4Smatthias.ringwald                 // for open:
12365a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1237fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1238c8e4258aSmatthias.ringwald             }
1239c8e4258aSmatthias.ringwald             break;
1240f62db1e3Smatthias.ringwald 
1241f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1242f62db1e3Smatthias.ringwald             switch (code) {
1243f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
124427a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
124527a923d0Smatthias.ringwald                     break;
12465a67bd4aSmatthias.ringwald                 default:
12475a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
12485a67bd4aSmatthias.ringwald                     break;
124927a923d0Smatthias.ringwald             }
125027a923d0Smatthias.ringwald             break;
125184836b65Smatthias.ringwald 
125284836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
125384836b65Smatthias.ringwald             // @TODO handle incoming requests
125484836b65Smatthias.ringwald             break;
125584836b65Smatthias.ringwald 
125684836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
125784836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
125884836b65Smatthias.ringwald             break;
125910642e45Smatthias.ringwald         default:
126010642e45Smatthias.ringwald             break;
126127a923d0Smatthias.ringwald     }
12629da54300S[email protected]     // log_info("new state %u", channel->state);
126327a923d0Smatthias.ringwald }
126427a923d0Smatthias.ringwald 
126500d93d79Smatthias.ringwald 
12667f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
126700d93d79Smatthias.ringwald 
126800d93d79Smatthias.ringwald     // get code, signalind identifier and command len
126900d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
127000d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
127100d93d79Smatthias.ringwald 
127200d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
127300d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
127463a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
127500d93d79Smatthias.ringwald         return;
127600d93d79Smatthias.ringwald     }
127700d93d79Smatthias.ringwald 
127800d93d79Smatthias.ringwald     // general commands without an assigned channel
127900d93d79Smatthias.ringwald     switch(code) {
128000d93d79Smatthias.ringwald 
128100d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1282f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1283f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
128400d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
12852b83fb7dSmatthias.ringwald             return;
128600d93d79Smatthias.ringwald         }
128700d93d79Smatthias.ringwald 
12882b360848Smatthias.ringwald         case ECHO_REQUEST:
12892b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
12902b83fb7dSmatthias.ringwald             return;
129100d93d79Smatthias.ringwald 
129200d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1293f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
12942b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
12952b83fb7dSmatthias.ringwald             return;
129600d93d79Smatthias.ringwald         }
129700d93d79Smatthias.ringwald 
129800d93d79Smatthias.ringwald         default:
129900d93d79Smatthias.ringwald             break;
130000d93d79Smatthias.ringwald     }
130100d93d79Smatthias.ringwald 
130200d93d79Smatthias.ringwald 
130300d93d79Smatthias.ringwald     // Get potential destination CID
1304f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
130500d93d79Smatthias.ringwald 
130600d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1307665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1308665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1309665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1310665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1311c22aecc9S[email protected]         if (channel->handle != handle) continue;
131200d93d79Smatthias.ringwald         if (code & 1) {
1313b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1314b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
131500d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13164e32727eSmatthias.ringwald                 break;
131700d93d79Smatthias.ringwald             }
131800d93d79Smatthias.ringwald         } else {
1319b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
132000d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
132100d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13224e32727eSmatthias.ringwald                 break;
132300d93d79Smatthias.ringwald             }
132400d93d79Smatthias.ringwald         }
132500d93d79Smatthias.ringwald     }
132600d93d79Smatthias.ringwald }
132700d93d79Smatthias.ringwald 
1328d9a7306aSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ){
132900d93d79Smatthias.ringwald 
133000d93d79Smatthias.ringwald     // Get Channel ID
133100d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
133200d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
133300d93d79Smatthias.ringwald 
13345652b5ffS[email protected]     switch (channel_id) {
13355652b5ffS[email protected] 
13365652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13375652b5ffS[email protected] 
133800d93d79Smatthias.ringwald             uint16_t command_offset = 8;
133900d93d79Smatthias.ringwald             while (command_offset < size) {
134000d93d79Smatthias.ringwald 
134100d93d79Smatthias.ringwald                 // handle signaling commands
134200d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
134300d93d79Smatthias.ringwald 
134400d93d79Smatthias.ringwald                 // increment command_offset
1345f8fbdce0SMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
134600d93d79Smatthias.ringwald             }
13475652b5ffS[email protected]             break;
134800d93d79Smatthias.ringwald         }
134900d93d79Smatthias.ringwald 
13505652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
13515652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
13525652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13535652b5ffS[email protected]             }
13545652b5ffS[email protected]             break;
13555652b5ffS[email protected] 
13565652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
13575652b5ffS[email protected]             if (security_protocol_packet_handler) {
13585652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13595652b5ffS[email protected]             }
13605652b5ffS[email protected]             break;
13615652b5ffS[email protected] 
1362462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1363462e630dS[email protected]             if (connectionless_channel_packet_handler) {
1364462e630dS[email protected]                 (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1365462e630dS[email protected]             }
1366462e630dS[email protected]             break;
1367462e630dS[email protected] 
13681bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1369ccf076adS[email protected]             switch (packet[8]){
1370ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1371f8fbdce0SMatthias Ringwald                     uint16_t result = little_endian_read_16(packet, 12);
1372ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1373ccf076adS[email protected]                     break;
1374ccf076adS[email protected]                 }
1375ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1376ccf076adS[email protected]                     uint8_t event[10];
1377ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1378ccf076adS[email protected]                     event[1] = 8;
1379ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1380da886c03S[email protected] 
1381da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1382da886c03S[email protected]                     if (connection){
13836d91fb6cSMatthias Ringwald                         if (connection->role != HCI_ROLE_MASTER){
13846d91fb6cSMatthias Ringwald                             // reject command without notifying upper layer when not in master role
13856d91fb6cSMatthias Ringwald                             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
13866d91fb6cSMatthias Ringwald                             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
13876d91fb6cSMatthias Ringwald                             break;
13886d91fb6cSMatthias Ringwald                         }
1389da886c03S[email protected]                         int update_parameter = 1;
1390a4c06b28SMatthias Ringwald                         le_connection_parameter_range_t existing_range;
1391a4c06b28SMatthias Ringwald                         gap_le_get_connection_parameter_range(existing_range);
1392f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_min = little_endian_read_16(packet,12);
1393f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_max = little_endian_read_16(packet,14);
1394f8fbdce0SMatthias Ringwald                         uint16_t le_conn_latency = little_endian_read_16(packet,16);
1395f8fbdce0SMatthias Ringwald                         uint16_t le_supervision_timeout = little_endian_read_16(packet,18);
1396da886c03S[email protected] 
1397da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1398da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1399da886c03S[email protected] 
1400da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1401da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1402da886c03S[email protected] 
1403da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1404da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1405da886c03S[email protected] 
1406da886c03S[email protected]                         if (update_parameter){
1407da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1408da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1409da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1410da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1411da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1412da886c03S[email protected]                         } else {
1413da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1414da886c03S[email protected]                         }
141579f53f1dSMatthias Ringwald                         connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1];
1416da886c03S[email protected]                     }
1417da886c03S[email protected] 
1418ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1419*33c40538SMatthias Ringwald                     if (!l2cap_event_packet_handler) break;
1420*33c40538SMatthias Ringwald                     (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1421ccf076adS[email protected]                     break;
1422ccf076adS[email protected]                 }
1423ccf076adS[email protected]                 default: {
14241bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14251bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14261bbc0b23S[email protected]                     break;
14271bbc0b23S[email protected]                 }
1428ccf076adS[email protected]             }
1429ccf076adS[email protected]             break;
1430ccf076adS[email protected]         }
14311bbc0b23S[email protected] 
14325652b5ffS[email protected]         default: {
143300d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
143400d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
143500d93d79Smatthias.ringwald             if (channel) {
143617a9b554SMatthias Ringwald                 l2cap_dispatch_to_channel(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
143700d93d79Smatthias.ringwald             }
14385652b5ffS[email protected]             break;
14395652b5ffS[email protected]         }
14405652b5ffS[email protected]     }
144100d93d79Smatthias.ringwald 
14421eb2563eS[email protected]     l2cap_run();
14432718e2e7Smatthias.ringwald }
144400d93d79Smatthias.ringwald 
144515ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
144627a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1447f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1448f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1449f62db1e3Smatthias.ringwald     // discard channel
14509dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1451665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1452d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1453c8e4258aSmatthias.ringwald }
14541e6aba47Smatthias.ringwald 
14558f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1456665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1457665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1458665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1459665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
14609d9bbc01Smatthias.ringwald         if ( service->psm == psm){
14619d9bbc01Smatthias.ringwald             return service;
14629d9bbc01Smatthias.ringwald         };
14639d9bbc01Smatthias.ringwald     }
14649d9bbc01Smatthias.ringwald     return NULL;
14659d9bbc01Smatthias.ringwald }
14669d9bbc01Smatthias.ringwald 
14677192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
14687192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
14697192e786SMatthias Ringwald }
14707192e786SMatthias Ringwald 
1471e0abb8e7S[email protected] 
1472be2053a6SMatthias 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){
1473be2053a6SMatthias Ringwald 
1474be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1475e0abb8e7S[email protected] 
14764bb582b6Smatthias.ringwald     // check for alread registered psm
14774bb582b6Smatthias.ringwald     // TODO: emit error event
14789d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1479277abc2cSmatthias.ringwald     if (service) {
1480be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1481be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1482277abc2cSmatthias.ringwald     }
14839d9bbc01Smatthias.ringwald 
14844bb582b6Smatthias.ringwald     // alloc structure
14854bb582b6Smatthias.ringwald     // TODO: emit error event
1486bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1487277abc2cSmatthias.ringwald     if (!service) {
1488be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1489be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1490277abc2cSmatthias.ringwald     }
14919d9bbc01Smatthias.ringwald 
14929d9bbc01Smatthias.ringwald     // fill in
14939d9bbc01Smatthias.ringwald     service->psm = psm;
14949d9bbc01Smatthias.ringwald     service->mtu = mtu;
149505ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1496df3354fcS[email protected]     service->required_security_level = security_level;
14979d9bbc01Smatthias.ringwald 
14989d9bbc01Smatthias.ringwald     // add to services list
1499665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1500c0e866bfSmatthias.ringwald 
1501c0e866bfSmatthias.ringwald     // enable page scan
1502c0e866bfSmatthias.ringwald     hci_connectable_control(1);
15035842b6d9Smatthias.ringwald 
1504be2053a6SMatthias Ringwald     return 0;
15059d9bbc01Smatthias.ringwald }
15069d9bbc01Smatthias.ringwald 
150702f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){
1508e0abb8e7S[email protected] 
1509e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1510e0abb8e7S[email protected] 
15119d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1512037d6e48Smatthias.ringwald     if (!service) return;
1513665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1514d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1515c0e866bfSmatthias.ringwald 
1516c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1517665d90f2SMatthias Ringwald     if (!btstack_linked_list_empty(&l2cap_services)) return;
1518c0e866bfSmatthias.ringwald     hci_connectable_control(0);
15199d9bbc01Smatthias.ringwald }
15209d9bbc01Smatthias.ringwald 
15215652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
152205ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
15235652b5ffS[email protected]     switch(channel_id){
15245652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
152505ae8de3SMatthias Ringwald             attribute_protocol_packet_handler = the_packet_handler;
15265652b5ffS[email protected]             break;
15275652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
152805ae8de3SMatthias Ringwald             security_protocol_packet_handler = the_packet_handler;
15295652b5ffS[email protected]             break;
1530462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
153105ae8de3SMatthias Ringwald             connectionless_channel_packet_handler = the_packet_handler;
1532462e630dS[email protected]             break;
15335652b5ffS[email protected]     }
15345652b5ffS[email protected] }
15355652b5ffS[email protected] 
1536a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
15377192e786SMatthias Ringwald 
15387f02f414SMatthias Ringwald 
15397f02f414SMatthias Ringwald #if 0
15407192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
15417192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
15427192e786SMatthias Ringwald }
15437192e786SMatthias Ringwald /**
15447192e786SMatthias Ringwald  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
15457192e786SMatthias Ringwald  * @param
15467192e786SMatthias Ringwald  */
1547ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm,
15487192e786SMatthias Ringwald     uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){
15497192e786SMatthias Ringwald 
15507192e786SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection);
15517192e786SMatthias Ringwald 
15527192e786SMatthias Ringwald     // check for alread registered psm
15537192e786SMatthias Ringwald     // TODO: emit error event
15547192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
15557192e786SMatthias Ringwald     if (service) {
15567192e786SMatthias Ringwald         log_error("l2cap_le_register_service_internal: PSM %u already registered", psm);
15577192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
15587192e786SMatthias Ringwald         return;
15597192e786SMatthias Ringwald     }
15607192e786SMatthias Ringwald 
15617192e786SMatthias Ringwald     // alloc structure
15627192e786SMatthias Ringwald     // TODO: emit error event
15637192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
15647192e786SMatthias Ringwald     if (!service) {
15657192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
15667192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
15677192e786SMatthias Ringwald         return;
15687192e786SMatthias Ringwald     }
15697192e786SMatthias Ringwald 
15707192e786SMatthias Ringwald     // fill in
15717192e786SMatthias Ringwald     service->psm = psm;
15727192e786SMatthias Ringwald     service->mtu = mtu;
15737192e786SMatthias Ringwald     service->mps = mps;
15747192e786SMatthias Ringwald     service->packet_handler = packet_handler;
15757192e786SMatthias Ringwald     service->required_security_level = security_level;
15767192e786SMatthias Ringwald 
15777192e786SMatthias Ringwald     // add to services list
1578665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
15797192e786SMatthias Ringwald 
15807192e786SMatthias Ringwald     // done
15817192e786SMatthias Ringwald     l2cap_emit_service_registered(connection, 0, psm);
15827192e786SMatthias Ringwald }
15837192e786SMatthias Ringwald 
1584ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) {
15857192e786SMatthias Ringwald 
15867192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
15877192e786SMatthias Ringwald 
15887192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
15897192e786SMatthias Ringwald     if (!service) return;
1590665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
15917192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
15927192e786SMatthias Ringwald }
15937192e786SMatthias Ringwald #endif
15947f02f414SMatthias Ringwald #endif
1595