xref: /btstack/src/l2cap.c (revision fb37a842ea98c26e62c79cddf7c8d9662590cf7e)
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 
69ffbf8201SMatthias Ringwald static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
7039bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
7139bda6d5Smatthias.ringwald 
7239bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
732b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
742b83fb7dSmatthias.ringwald static int signaling_responses_pending;
752b83fb7dSmatthias.ringwald 
76*fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
77*fb37a842SMatthias Ringwald 
788f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
798f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_services;
808f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
818f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
82ffbf8201SMatthias Ringwald static void (*packet_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
831e6aba47Smatthias.ringwald 
84c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler;
85c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler;
86462e630dS[email protected] static btstack_packet_handler_t connectionless_channel_packet_handler;
87ac301f95S[email protected] static uint8_t require_security_level2_for_outgoing_sdp;
885652b5ffS[email protected] 
8939bda6d5Smatthias.ringwald // prototypes
90fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
919c9876e7SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
92fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
93fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
94fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
95fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
9639bda6d5Smatthias.ringwald 
9739bda6d5Smatthias.ringwald 
9871de195eSMatthias Ringwald void l2cap_init(void){
992b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
100808a48abSmatthias.ringwald 
101f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
102f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1037192e786SMatthias Ringwald     l2cap_le_services = NULL;
1047192e786SMatthias Ringwald     l2cap_le_channels = NULL;
105f5454fc6Smatthias.ringwald 
106f5454fc6Smatthias.ringwald     packet_handler = null_packet_handler;
107c42e2ff2S[email protected]     attribute_protocol_packet_handler = NULL;
108c42e2ff2S[email protected]     security_protocol_packet_handler = NULL;
109462e630dS[email protected]     connectionless_channel_packet_handler = NULL;
110f5454fc6Smatthias.ringwald 
111ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
112ac301f95S[email protected] 
113fcadd0caSmatthias.ringwald     //
1142718e2e7Smatthias.ringwald     // register callback with HCI
115fcadd0caSmatthias.ringwald     //
116*fb37a842SMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_packet_handler;
117*fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
118*fb37a842SMatthias Ringwald 
119*fb37a842SMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_packet_handler);
120*fb37a842SMatthias Ringwald 
121c0e866bfSmatthias.ringwald     hci_connectable_control(0); // no services yet
122fcadd0caSmatthias.ringwald }
123fcadd0caSmatthias.ringwald 
124fcadd0caSmatthias.ringwald 
125fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
126ffbf8201SMatthias Ringwald static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
127fcadd0caSmatthias.ringwald }
128ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
129b502e1b0Smatthias.ringwald     packet_handler = handler;
1301e6aba47Smatthias.ringwald }
1311e6aba47Smatthias.ringwald 
13258de5610Smatthias.ringwald //  notify client/protocol handler
1337f02f414SMatthias Ringwald static void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
13458de5610Smatthias.ringwald     if (channel->packet_handler) {
13558de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
13658de5610Smatthias.ringwald     } else {
137ffbf8201SMatthias Ringwald         (*packet_handler)(type, channel->local_cid, data, size);
13858de5610Smatthias.ringwald     }
13958de5610Smatthias.ringwald }
14058de5610Smatthias.ringwald 
14158de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
142c9dc710bS[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",
143e0abb8e7S[email protected]              status, bd_addr_to_str(channel->address), channel->handle, channel->psm,
144c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
145c9dc710bS[email protected]     uint8_t event[23];
14658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
14758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14858de5610Smatthias.ringwald     event[2] = status;
14958de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
150f8fbdce0SMatthias Ringwald     little_endian_store_16(event,  9, channel->handle);
151f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
152f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
153f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
154f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
155f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
156f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
15758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15858de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
15958de5610Smatthias.ringwald }
16058de5610Smatthias.ringwald 
16158de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
162e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
16358de5610Smatthias.ringwald     uint8_t event[4];
16458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
16558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
166f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
16758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
16858de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
16958de5610Smatthias.ringwald }
17058de5610Smatthias.ringwald 
17158de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
172e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
173e0abb8e7S[email protected]              bd_addr_to_str(channel->address), channel->handle,  channel->psm, channel->local_cid, channel->remote_cid);
17458de5610Smatthias.ringwald     uint8_t event[16];
17558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
17658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
17758de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
178f8fbdce0SMatthias Ringwald     little_endian_store_16(event,  8, channel->handle);
179f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
180f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
181f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
18258de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18358de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1840af41d30Smatthias.ringwald }
185808a48abSmatthias.ringwald 
1867f02f414SMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){
187ccf076adS[email protected]     uint8_t event[6];
188ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
189ccf076adS[email protected]     event[1] = 4;
190f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, handle);
191f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
192ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
193ffbf8201SMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
194ccf076adS[email protected] }
195ccf076adS[email protected] 
1967f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
197665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
198665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
199665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
200665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
201b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
202f62db1e3Smatthias.ringwald             return channel;
203f62db1e3Smatthias.ringwald         }
204f62db1e3Smatthias.ringwald     }
205f62db1e3Smatthias.ringwald     return NULL;
206f62db1e3Smatthias.ringwald }
207f62db1e3Smatthias.ringwald 
2086b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2096b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2106b1fde37Smatthias.ringwald     if (!channel) return 0;
211a35252c8S[email protected]     return hci_can_send_acl_packet_now(channel->handle);
2127856fb31S[email protected] }
2137856fb31S[email protected] 
21483e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
21583e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
21683e7cdd9SMatthias Ringwald     if (!channel) return 0;
21783e7cdd9SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->handle);
21883e7cdd9SMatthias Ringwald }
21983e7cdd9SMatthias Ringwald 
2206cd4da6bS[email protected] int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle){
2216cd4da6bS[email protected]     return hci_can_send_acl_packet_now(handle);
2223cab4fcaS[email protected] }
2233cab4fcaS[email protected] 
22496cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
22596cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
22696cbd662Smatthias.ringwald     if (channel) {
22796cbd662Smatthias.ringwald         return channel->remote_mtu;
22896cbd662Smatthias.ringwald     }
22996cbd662Smatthias.ringwald     return 0;
23096cbd662Smatthias.ringwald }
23196cbd662Smatthias.ringwald 
232ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
233665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
234665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
235665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
236665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2375932bd7cS[email protected]         if ( &channel->rtx == ts) {
2385932bd7cS[email protected]             return channel;
2395932bd7cS[email protected]         }
2405932bd7cS[email protected]     }
2415932bd7cS[email protected]     return NULL;
2425932bd7cS[email protected] }
2435932bd7cS[email protected] 
244ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
2455932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2465932bd7cS[email protected]     if (!ts) return;
2475932bd7cS[email protected] 
2485932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2495932bd7cS[email protected] 
2505932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
2515932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
2525932bd7cS[email protected]     // notify client
2535932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
2545932bd7cS[email protected] 
2555932bd7cS[email protected]     // discard channel
2569dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
257665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
2585932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
2595932bd7cS[email protected] }
2605932bd7cS[email protected] 
2615932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
2625932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
263528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
2645932bd7cS[email protected] }
2655932bd7cS[email protected] 
2665932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
2675932bd7cS[email protected]     l2cap_stop_rtx(channel);
268cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
269528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
270528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
271528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
2725932bd7cS[email protected] }
2735932bd7cS[email protected] 
2745932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
2755932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
2765932bd7cS[email protected]     l2cap_stop_rtx(channel);
277528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
278528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
279528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
2805932bd7cS[email protected] }
2815932bd7cS[email protected] 
28271de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
283ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
284ac301f95S[email protected] }
285ac301f95S[email protected] 
286df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
287ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
288df3354fcS[email protected] }
2895932bd7cS[email protected] 
2907f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
291b1d43497Smatthias.ringwald 
292a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
2939da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
294b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
295b1d43497Smatthias.ringwald     }
296b1d43497Smatthias.ringwald 
2979da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
2982a373862S[email protected]     hci_reserve_packet_buffer();
299facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
30058de5610Smatthias.ringwald     va_list argptr;
30158de5610Smatthias.ringwald     va_start(argptr, identifier);
30270efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
30358de5610Smatthias.ringwald     va_end(argptr);
3049da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
305826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
30658de5610Smatthias.ringwald }
30758de5610Smatthias.ringwald 
308a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3097f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
31070efece1S[email protected] 
311a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3129da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
31370efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
31470efece1S[email protected]     }
31570efece1S[email protected] 
3169da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3172a373862S[email protected]     hci_reserve_packet_buffer();
318facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
31970efece1S[email protected]     va_list argptr;
32070efece1S[email protected]     va_start(argptr, identifier);
32170efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
32270efece1S[email protected]     va_end(argptr);
3239da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
324826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
32570efece1S[email protected] }
32670efece1S[email protected] #endif
32770efece1S[email protected] 
328b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
329facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
330b1d43497Smatthias.ringwald }
3316218e6f1Smatthias.ringwald 
3326b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3336b4af23dS[email protected]     return hci_reserve_packet_buffer();
3346b4af23dS[email protected] }
3356b4af23dS[email protected] 
33668a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
33768a0fcf7S[email protected]     hci_release_packet_buffer();
33868a0fcf7S[email protected] }
33968a0fcf7S[email protected] 
34068a0fcf7S[email protected] 
341b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
342b1d43497Smatthias.ringwald 
343c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
344c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
345c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
346c8b9416aS[email protected]     }
347c8b9416aS[email protected] 
34858de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
349b1d43497Smatthias.ringwald     if (!channel) {
3509da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
351b1d43497Smatthias.ringwald         return -1;   // TODO: define error
3526218e6f1Smatthias.ringwald     }
3536218e6f1Smatthias.ringwald 
354a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(channel->handle)){
3559da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
356a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
357a35252c8S[email protected]     }
358a35252c8S[email protected] 
3593aff022fSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle);
360b1d43497Smatthias.ringwald 
361facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
362b1d43497Smatthias.ringwald 
363e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
364e9772277S[email protected] 
365e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
366f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14));
36758de5610Smatthias.ringwald     // 2 - ACL length
368f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
36958de5610Smatthias.ringwald     // 4 - L2CAP packet length
370f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
37158de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
372f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
37358de5610Smatthias.ringwald     // send
374826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
37591b99603Smatthias.ringwald 
3766218e6f1Smatthias.ringwald     return err;
37758de5610Smatthias.ringwald }
37858de5610Smatthias.ringwald 
3792149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
3802149f12eSmatthias.ringwald 
381c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
382c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
3832149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3842149f12eSmatthias.ringwald     }
3852149f12eSmatthias.ringwald 
386a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(handle)){
3879da54300S[email protected]         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid);
388c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
389c8b9416aS[email protected]     }
390c8b9416aS[email protected] 
3919da54300S[email protected]     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid);
3922149f12eSmatthias.ringwald 
393facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
3942149f12eSmatthias.ringwald 
395e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
396e9772277S[email protected] 
397e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
398f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
3992149f12eSmatthias.ringwald     // 2 - ACL length
400f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4012149f12eSmatthias.ringwald     // 4 - L2CAP packet length
402f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4032149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
404f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4052149f12eSmatthias.ringwald     // send
406826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4072149f12eSmatthias.ringwald 
4082149f12eSmatthias.ringwald     return err;
4092149f12eSmatthias.ringwald }
4102149f12eSmatthias.ringwald 
411ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
412b1d43497Smatthias.ringwald 
413a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
414a35252c8S[email protected]     if (!channel) {
415ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
416a35252c8S[email protected]         return -1;   // TODO: define error
417a35252c8S[email protected]     }
418a35252c8S[email protected] 
419f0efaa57S[email protected]     if (len > channel->remote_mtu){
420ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
421f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
422f0efaa57S[email protected]     }
423f0efaa57S[email protected] 
424a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(channel->handle)){
425ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
426b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
427b1d43497Smatthias.ringwald     }
428b1d43497Smatthias.ringwald 
4292a373862S[email protected]     hci_reserve_packet_buffer();
430facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
431b1d43497Smatthias.ringwald 
432b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
433b1d43497Smatthias.ringwald 
434b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
435b1d43497Smatthias.ringwald }
436b1d43497Smatthias.ringwald 
4372149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
4382149f12eSmatthias.ringwald 
439a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
440ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
4412149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4422149f12eSmatthias.ringwald     }
4432149f12eSmatthias.ringwald 
4442a373862S[email protected]     hci_reserve_packet_buffer();
445facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4462149f12eSmatthias.ringwald 
4472149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4482149f12eSmatthias.ringwald 
4492149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4502149f12eSmatthias.ringwald }
4512149f12eSmatthias.ringwald 
4520e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){
4530e37e417S[email protected]     return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data);
4540e37e417S[email protected] }
4550e37e417S[email protected] 
45628ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
45728ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
45828ca2b46S[email protected] }
45928ca2b46S[email protected] 
46028ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
46128ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
46228ca2b46S[email protected] }
46328ca2b46S[email protected] 
46428ca2b46S[email protected] 
465b1d43497Smatthias.ringwald 
4668158c421Smatthias.ringwald // MARK: L2CAP_RUN
4672cd0be45Smatthias.ringwald // process outstanding signaling tasks
4687f02f414SMatthias Ringwald static void l2cap_run(void){
4692b83fb7dSmatthias.ringwald 
47022c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
47122c29ab4SMatthias Ringwald 
4722b83fb7dSmatthias.ringwald     // check pending signaling responses
4732b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
4742b83fb7dSmatthias.ringwald 
4752b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
476a35252c8S[email protected] 
477a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
478a35252c8S[email protected] 
4792b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
4802b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
48163a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
482f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
4832b83fb7dSmatthias.ringwald 
484f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
485f53da564S[email protected]         signaling_responses_pending--;
486f53da564S[email protected]         int i;
487f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
488f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
489f53da564S[email protected]         }
490f53da564S[email protected] 
491f53da564S[email protected]         switch (response_code){
4922b360848Smatthias.ringwald             case CONNECTION_REQUEST:
4932b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
4942bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
4954d816277S[email protected]                 if (result == 0x0003){
4962bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
4974d816277S[email protected]                 }
4982b360848Smatthias.ringwald                 break;
4992b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5002b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5012b83fb7dSmatthias.ringwald                 break;
5022b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5033b0484b3S[email protected]                 switch (infoType){
5043b0484b3S[email protected]                     case 1: { // Connectionless MTU
5053b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5063b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5073b0484b3S[email protected]                         break;
5083b0484b3S[email protected]                     }
5093b0484b3S[email protected]                     case 2: { // Extended Features Supported
510462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
511462e630dS[email protected]                         uint32_t features = 0x280;
5123b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5133b0484b3S[email protected]                         break;
5143b0484b3S[email protected]                     }
5153b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5163b0484b3S[email protected]                         uint8_t map[8];
5173b0484b3S[email protected]                         memset(map, 0, 8);
518462e630dS[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02)
5193b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5203b0484b3S[email protected]                         break;
5213b0484b3S[email protected]                     }
5223b0484b3S[email protected]                     default:
5232b83fb7dSmatthias.ringwald                         // all other types are not supported
5242b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5253b0484b3S[email protected]                         break;
5262b83fb7dSmatthias.ringwald                 }
5272b83fb7dSmatthias.ringwald                 break;
52863a7246aSmatthias.ringwald             case COMMAND_REJECT:
5295ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
530a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
53170efece1S[email protected]             case COMMAND_REJECT_LE:
53270efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
53363a7246aSmatthias.ringwald                 break;
53470efece1S[email protected] #endif
5352b83fb7dSmatthias.ringwald             default:
5362b83fb7dSmatthias.ringwald                 // should not happen
5372b83fb7dSmatthias.ringwald                 break;
5382b83fb7dSmatthias.ringwald         }
5392b83fb7dSmatthias.ringwald     }
5402b83fb7dSmatthias.ringwald 
541ae280e73Smatthias.ringwald     uint8_t  config_options[4];
542665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
543665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
544665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
545baf94f06S[email protected] 
546665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
54722c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
5482cd0be45Smatthias.ringwald         switch (channel->state){
5492cd0be45Smatthias.ringwald 
550df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
551ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
552baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
553a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
554ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
555a00031e2S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
556ad671560S[email protected]                 }
557ad671560S[email protected]                 break;
558ad671560S[email protected] 
55902b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
560baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
56164472d52Smatthias.ringwald                 // send connection request - set state first
56264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
56302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
5648f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
56502b22dc4Smatthias.ringwald                 break;
56602b22dc4Smatthias.ringwald 
567e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
568baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
56922c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
5701eb2563eS[email protected]                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
571e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
5729dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
573665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
574d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
575e7ff783cSmatthias.ringwald                 break;
576e7ff783cSmatthias.ringwald 
577552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
578baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
579fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
58028ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
5812a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
582552d92a1Smatthias.ringwald                 break;
583552d92a1Smatthias.ringwald 
5846fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
585baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
5866fdcc387Smatthias.ringwald                 // success, start l2cap handshake
587b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
5886fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
5892a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
5905932bd7cS[email protected]                 l2cap_start_rtx(channel);
5916fdcc387Smatthias.ringwald                 break;
5926fdcc387Smatthias.ringwald 
593fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
594baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
59573cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
59663a7246aSmatthias.ringwald                     uint16_t flags = 0;
59728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
59863a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
59963a7246aSmatthias.ringwald                         flags = 1;
60063a7246aSmatthias.ringwald                     } else {
60128ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
60263a7246aSmatthias.ringwald                     }
60363a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
60463a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
60563a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
60663a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
60763a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
608f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
60963a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
61063a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
61163a7246aSmatthias.ringwald                     } else {
61263a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
61363a7246aSmatthias.ringwald                     }
61463a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
615fa8473a4Smatthias.ringwald                 }
61673cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
61728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
61828ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
619b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
620ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
621ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
622f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
623b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6245932bd7cS[email protected]                     l2cap_start_rtx(channel);
625fa8473a4Smatthias.ringwald                 }
626fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
627552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
628552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
629fa8473a4Smatthias.ringwald                 }
630552d92a1Smatthias.ringwald                 break;
631552d92a1Smatthias.ringwald 
632e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
633baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
63422c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
635b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6365932bd7cS[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 :)
637756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
638e7ff783cSmatthias.ringwald                 break;
639e7ff783cSmatthias.ringwald 
640e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
641baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
642b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6432cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
6442a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
6452cd0be45Smatthias.ringwald                 break;
6462cd0be45Smatthias.ringwald             default:
6472cd0be45Smatthias.ringwald                 break;
6482cd0be45Smatthias.ringwald         }
6492cd0be45Smatthias.ringwald     }
650da886c03S[email protected] 
651a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
652da886c03S[email protected]     // send l2cap con paramter update if necessary
653da886c03S[email protected]     hci_connections_get_iterator(&it);
654665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
655665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
6565d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
657b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
658da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
659b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
660b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
661b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
662b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
663b68d7bc3SMatthias Ringwald                 break;
664da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
665b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
666b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
667da886c03S[email protected]                 break;
668da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
669b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
670b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
671da886c03S[email protected]                 break;
672da886c03S[email protected]             default:
673da886c03S[email protected]                 break;
674da886c03S[email protected]         }
675da886c03S[email protected]     }
6764d7157c3S[email protected] #endif
677da886c03S[email protected] 
67822c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
6792cd0be45Smatthias.ringwald }
6802cd0be45Smatthias.ringwald 
6814aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
6824ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
683fa8c92f6Smatthias.ringwald }
684fa8c92f6Smatthias.ringwald 
68571de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
6864ff786cfS[email protected]     return l2cap_max_mtu();
687e5e1518dS[email protected] }
688e5e1518dS[email protected] 
6892df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){
6902df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
6915533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
6922df5dadcS[email protected]         // success, start l2cap handshake
6932df5dadcS[email protected]         channel->handle = handle;
6942df5dadcS[email protected]         // check remote SSP feature first
6952df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
6962df5dadcS[email protected]     }
6972df5dadcS[email protected] }
6982df5dadcS[email protected] 
6992df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7002df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7012df5dadcS[email protected] 
7022df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
703ac301f95S[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));
7042df5dadcS[email protected]     if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7052df5dadcS[email protected]         // request security level 2
7062df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
7071429b2d6S[email protected]         gap_request_security_level(channel->handle, LEVEL_2);
7082df5dadcS[email protected]         return;
7092df5dadcS[email protected]     }
7102df5dadcS[email protected]     // fine, go ahead
7112df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7122df5dadcS[email protected] }
7132df5dadcS[email protected] 
7149077cb15SMatthias Ringwald /**
7159077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
7169077cb15SMatthias Ringwald  * @param packet_handler
7179077cb15SMatthias Ringwald  * @param address
7189077cb15SMatthias Ringwald  * @param psm
7199077cb15SMatthias Ringwald  * @param mtu
7209077cb15SMatthias Ringwald  * @param local_cid
7219077cb15SMatthias Ringwald  */
7229077cb15SMatthias 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){
7239077cb15SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
7249077cb15SMatthias Ringwald 
7259077cb15SMatthias Ringwald     // alloc structure
7269077cb15SMatthias Ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
7279077cb15SMatthias Ringwald     if (!chan) {
7289077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
7299077cb15SMatthias Ringwald     }
7309077cb15SMatthias Ringwald 
7319077cb15SMatthias Ringwald      // Init memory (make valgrind happy)
7329077cb15SMatthias Ringwald     memset(chan, 0, sizeof(l2cap_channel_t));
7339077cb15SMatthias Ringwald     // limit local mtu to max acl packet length - l2cap header
7349077cb15SMatthias Ringwald     if (mtu > l2cap_max_mtu()) {
7359077cb15SMatthias Ringwald         mtu = l2cap_max_mtu();
7369077cb15SMatthias Ringwald     }
7379077cb15SMatthias Ringwald 
7389077cb15SMatthias Ringwald     // fill in
7399077cb15SMatthias Ringwald     BD_ADDR_COPY(chan->address, address);
7409077cb15SMatthias Ringwald     chan->psm = psm;
7419077cb15SMatthias Ringwald     chan->handle = 0;
7429077cb15SMatthias Ringwald     chan->packet_handler = channel_packet_handler;
7439077cb15SMatthias Ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
7449077cb15SMatthias Ringwald     chan->local_mtu = mtu;
7459077cb15SMatthias Ringwald     chan->local_cid = l2cap_next_local_cid();
7469077cb15SMatthias Ringwald 
7479077cb15SMatthias Ringwald     // set initial state
7489077cb15SMatthias Ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
7499077cb15SMatthias Ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
7509077cb15SMatthias Ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
7519077cb15SMatthias Ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
7529077cb15SMatthias Ringwald     chan->required_security_level = LEVEL_0;
7539077cb15SMatthias Ringwald 
7549077cb15SMatthias Ringwald     // add to connections list
755665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan);
7569077cb15SMatthias Ringwald 
7579077cb15SMatthias Ringwald     // store local_cid
7589077cb15SMatthias Ringwald     if (out_local_cid){
7599077cb15SMatthias Ringwald        *out_local_cid = chan->local_cid;
7609077cb15SMatthias Ringwald     }
7619077cb15SMatthias Ringwald 
7629077cb15SMatthias Ringwald     // check if hci connection is already usable
7639077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
7649077cb15SMatthias Ringwald     if (conn){
765add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
7669077cb15SMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, chan);
7679077cb15SMatthias Ringwald         // check if remote supported fearures are already received
7689077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
7699077cb15SMatthias Ringwald             l2cap_handle_remote_supported_features_received(chan);
7709077cb15SMatthias Ringwald         }
7719077cb15SMatthias Ringwald     }
7729077cb15SMatthias Ringwald 
7739077cb15SMatthias Ringwald     l2cap_run();
7749077cb15SMatthias Ringwald 
7759077cb15SMatthias Ringwald     return 0;
7769077cb15SMatthias Ringwald }
7779077cb15SMatthias Ringwald 
778ce8f182eSMatthias Ringwald void
779ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
780e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
781b35f641cSmatthias.ringwald     // find channel for local_cid
782b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
783f62db1e3Smatthias.ringwald     if (channel) {
784e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
785f62db1e3Smatthias.ringwald     }
7862cd0be45Smatthias.ringwald     // process
7872cd0be45Smatthias.ringwald     l2cap_run();
78843625864Smatthias.ringwald }
7891e6aba47Smatthias.ringwald 
790afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
791665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
792665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
793665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
794665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
795c22aecc9S[email protected]         if ( BD_ADDR_CMP( channel->address, address) != 0) continue;
796c22aecc9S[email protected]         // channel for this address found
797c22aecc9S[email protected]         switch (channel->state){
798c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
799c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
800afde0c52Smatthias.ringwald                 // failure, forward error code
801afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
802afde0c52Smatthias.ringwald                 // discard channel
8039dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
804665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
805d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
806c22aecc9S[email protected]                 break;
807c22aecc9S[email protected]             default:
808c22aecc9S[email protected]                 break;
809afde0c52Smatthias.ringwald         }
810afde0c52Smatthias.ringwald     }
811afde0c52Smatthias.ringwald }
812afde0c52Smatthias.ringwald 
813afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
814665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
815665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
816665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
817665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
818afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
8192df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
820afde0c52Smatthias.ringwald         }
821afde0c52Smatthias.ringwald     }
8226fdcc387Smatthias.ringwald     // process
8236fdcc387Smatthias.ringwald     l2cap_run();
824afde0c52Smatthias.ringwald }
825b448a0e7Smatthias.ringwald 
8267f02f414SMatthias Ringwald static void l2cap_event_handler(uint8_t *packet, uint16_t size){
827afde0c52Smatthias.ringwald 
828afde0c52Smatthias.ringwald     bd_addr_t address;
829afde0c52Smatthias.ringwald     hci_con_handle_t handle;
830665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
8312d00edd4Smatthias.ringwald     int hci_con_used;
832afde0c52Smatthias.ringwald 
833afde0c52Smatthias.ringwald     switch(packet[0]){
834afde0c52Smatthias.ringwald 
835afde0c52Smatthias.ringwald         // handle connection complete events
836afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
837afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
838afde0c52Smatthias.ringwald             if (packet[2] == 0){
839f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
840afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
841afde0c52Smatthias.ringwald             } else {
842afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
843afde0c52Smatthias.ringwald             }
844afde0c52Smatthias.ringwald             break;
845afde0c52Smatthias.ringwald 
846afde0c52Smatthias.ringwald         // handle successful create connection cancel command
847afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
848afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
849afde0c52Smatthias.ringwald                 if (packet[5] == 0){
850afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
851afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
852afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
85303cfbabcSmatthias.ringwald                 }
8541e6aba47Smatthias.ringwald             }
85539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
85639d59809Smatthias.ringwald             break;
85739d59809Smatthias.ringwald 
85839d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
85939d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
860afde0c52Smatthias.ringwald             break;
86127a923d0Smatthias.ringwald 
8621e6aba47Smatthias.ringwald         // handle disconnection complete events
863afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
864c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
865f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
866665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
867665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
868665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
869c22aecc9S[email protected]                 if (channel->handle != handle) continue;
87015ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
8719dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
872665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
873d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
87427a923d0Smatthias.ringwald             }
875afde0c52Smatthias.ringwald             break;
876fcadd0caSmatthias.ringwald 
8776218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
87802b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
8796218e6f1Smatthias.ringwald             break;
8806218e6f1Smatthias.ringwald 
881ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
882afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
883f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
884bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
88580ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
8862d00edd4Smatthias.ringwald             hci_con_used = 0;
887665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
888665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
889665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
890c22aecc9S[email protected]                 if (channel->handle != handle) continue;
8912d00edd4Smatthias.ringwald                 hci_con_used = 1;
892c22aecc9S[email protected]                 break;
893ee091cf1Smatthias.ringwald             }
8942d00edd4Smatthias.ringwald             if (hci_con_used) break;
895d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
8969edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
897afde0c52Smatthias.ringwald             break;
898ee091cf1Smatthias.ringwald 
8996e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
900b26c3003SMatthias Ringwald             l2cap_run();    // try sending signaling packets first
901b26c3003SMatthias Ringwald 
902665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
903665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
904665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
905c22aecc9S[email protected]                 if (!channel->packet_handler) continue;
9066e6710ebSmatthias.ringwald                 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
9076e6710ebSmatthias.ringwald             }
9085652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
9095652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
9105652b5ffS[email protected]             }
9115652b5ffS[email protected]             if (security_protocol_packet_handler) {
912133efcfdSmatthias.ringwald                 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
9135652b5ffS[email protected]             }
914462e630dS[email protected]             if (connectionless_channel_packet_handler) {
915462e630dS[email protected]                 (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
916462e630dS[email protected]             }
9176e6710ebSmatthias.ringwald             break;
9186e6710ebSmatthias.ringwald 
919df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
920f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
921665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
922665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
923665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
924df3354fcS[email protected]                 if (channel->handle != handle) continue;
9252df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
926df3354fcS[email protected]                 break;
927df3354fcS[email protected]             }
928c22aecc9S[email protected]             break;
929df3354fcS[email protected] 
9305611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
931f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
932bd63148eS[email protected]             log_info("l2cap - security level update");
933665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
934665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
935665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
936f85a9399S[email protected]                 if (channel->handle != handle) continue;
9375533f01eS[email protected] 
938bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
939bd63148eS[email protected] 
940e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
9415533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
9425533f01eS[email protected] 
943df3354fcS[email protected]                 switch (channel->state){
944df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
9455533f01eS[email protected]                         if (actual_level >= required_level){
946f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
947f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
9481eb2563eS[email protected]                         } else {
949775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
9501eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
9511eb2563eS[email protected]                         }
952df3354fcS[email protected]                         break;
953df3354fcS[email protected] 
954df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
9555533f01eS[email protected]                         if (actual_level >= required_level){
956df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
957df3354fcS[email protected]                         } else {
958df3354fcS[email protected]                             // disconnnect, authentication not good enough
959df3354fcS[email protected]                             hci_disconnect_security_block(handle);
960df3354fcS[email protected]                         }
961df3354fcS[email protected]                         break;
962df3354fcS[email protected] 
963df3354fcS[email protected]                     default:
964df3354fcS[email protected]                         break;
965df3354fcS[email protected]                 }
966f85a9399S[email protected]             }
967f85a9399S[email protected]             break;
968f85a9399S[email protected] 
969afde0c52Smatthias.ringwald         default:
970afde0c52Smatthias.ringwald             break;
971afde0c52Smatthias.ringwald     }
972afde0c52Smatthias.ringwald 
973e0707417S[email protected]     // pass on: main packet handler, att and sm packet handlers
974ffbf8201SMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
975e0707417S[email protected]     if (attribute_protocol_packet_handler){
976e0707417S[email protected]         (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
977e0707417S[email protected]     }
978e0707417S[email protected]     if (security_protocol_packet_handler) {
979e0707417S[email protected]         (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
980e0707417S[email protected]     }
981462e630dS[email protected]     if (connectionless_channel_packet_handler) {
982462e630dS[email protected]         (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
983462e630dS[email protected]     }
984bd63148eS[email protected] 
985bd63148eS[email protected]     l2cap_run();
9861e6aba47Smatthias.ringwald }
9871e6aba47Smatthias.ringwald 
988afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
989b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
990e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
991e7ff783cSmatthias.ringwald     l2cap_run();
99284836b65Smatthias.ringwald }
99384836b65Smatthias.ringwald 
9942b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
9954cf56b4aSmatthias.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."
9962b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
9972b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
9982b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
9992b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
10002b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
10012b360848Smatthias.ringwald         signaling_responses_pending++;
10022b360848Smatthias.ringwald         l2cap_run();
10032b360848Smatthias.ringwald     }
10042b360848Smatthias.ringwald }
10052b360848Smatthias.ringwald 
1006b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1007645658c9Smatthias.ringwald 
10089da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1009645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1010645658c9Smatthias.ringwald     if (!service) {
1011645658c9Smatthias.ringwald         // 0x0002 PSM not supported
10122b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1013645658c9Smatthias.ringwald         return;
1014645658c9Smatthias.ringwald     }
1015645658c9Smatthias.ringwald 
10165061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1017645658c9Smatthias.ringwald     if (!hci_connection) {
10182b360848Smatthias.ringwald         //
10199da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1020645658c9Smatthias.ringwald         return;
1021645658c9Smatthias.ringwald     }
10222bd8b7e7S[email protected] 
1023645658c9Smatthias.ringwald     // alloc structure
10249da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1025bb69aaaeS[email protected]     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
10262b360848Smatthias.ringwald     if (!channel){
10272b360848Smatthias.ringwald         // 0x0004 No resources available
10282b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10292b360848Smatthias.ringwald         return;
10302b360848Smatthias.ringwald     }
1031c523d53dS[email protected]     // Init memory (make valgrind happy)
1032c523d53dS[email protected]     memset(channel, 0, sizeof(l2cap_channel_t));
1033645658c9Smatthias.ringwald     // fill in
1034169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
1035169f8b28Smatthias.ringwald     channel->psm = psm;
1036169f8b28Smatthias.ringwald     channel->handle = handle;
1037f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1038b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1039b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1040fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
10410a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1042b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1043df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1044645658c9Smatthias.ringwald 
1045f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
10462985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
10472985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
10489775e25bSmatthias.ringwald     }
10499775e25bSmatthias.ringwald 
1050645658c9Smatthias.ringwald     // set initial state
1051df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1052ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1053e405ae81Smatthias.ringwald 
1054645658c9Smatthias.ringwald     // add to connections list
1055665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1056645658c9Smatthias.ringwald 
1057f85a9399S[email protected]     // assert security requirements
10581eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1059e405ae81Smatthias.ringwald }
1060645658c9Smatthias.ringwald 
1061ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1062e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1063b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1064e405ae81Smatthias.ringwald     if (!channel) {
1065ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1066e405ae81Smatthias.ringwald         return;
1067e405ae81Smatthias.ringwald     }
1068e405ae81Smatthias.ringwald 
1069552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1070e405ae81Smatthias.ringwald 
1071552d92a1Smatthias.ringwald     // process
1072552d92a1Smatthias.ringwald     l2cap_run();
1073e405ae81Smatthias.ringwald }
1074645658c9Smatthias.ringwald 
1075ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){
1076e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1077b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1078e405ae81Smatthias.ringwald     if (!channel) {
1079ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1080e405ae81Smatthias.ringwald         return;
1081e405ae81Smatthias.ringwald     }
1082e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1083e7ff783cSmatthias.ringwald     channel->reason = reason;
1084e7ff783cSmatthias.ringwald     l2cap_run();
1085645658c9Smatthias.ringwald }
1086645658c9Smatthias.ringwald 
10877f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1088b1988dceSmatthias.ringwald 
1089b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1090b1988dceSmatthias.ringwald 
1091f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
109263a7246aSmatthias.ringwald     if (flags & 1) {
109363a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
109463a7246aSmatthias.ringwald     }
109563a7246aSmatthias.ringwald 
10962784b77dSmatthias.ringwald     // accept the other's configuration options
1097f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
10983de7c0caSmatthias.ringwald     uint16_t pos     = 8;
10993de7c0caSmatthias.ringwald     while (pos < end_pos){
110063a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
110163a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
110263a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
110363a7246aSmatthias.ringwald         pos++;
11041dc511deSmatthias.ringwald         uint8_t length = command[pos++];
11051dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
110663a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1107f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
11089da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
110963a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
111063a7246aSmatthias.ringwald         }
11110fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11120fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1113f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
11140fe7a9d0S[email protected]         }
111563a7246aSmatthias.ringwald         // check for unknown options
111663a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1117c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
111863a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11191dc511deSmatthias.ringwald         }
11201dc511deSmatthias.ringwald         pos += length;
11211dc511deSmatthias.ringwald     }
11222784b77dSmatthias.ringwald }
11232784b77dSmatthias.ringwald 
1124fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11259da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
112673cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
112773cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1128019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1129019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1130fa8473a4Smatthias.ringwald     return 1;
1131fa8473a4Smatthias.ringwald }
1132fa8473a4Smatthias.ringwald 
1133fa8473a4Smatthias.ringwald 
11347f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11351e6aba47Smatthias.ringwald 
113600d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
113700d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
113838e5900eSmatthias.ringwald     uint16_t result = 0;
11391e6aba47Smatthias.ringwald 
11409da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1141b35f641cSmatthias.ringwald 
11429a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
11439a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
11449a011532Smatthias.ringwald         switch (channel->state){
1145fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
11469a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
11472b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
11489a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
11499a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
11509a011532Smatthias.ringwald                 break;
11519a011532Smatthias.ringwald 
11529a011532Smatthias.ringwald             default:
11539a011532Smatthias.ringwald                 // ignore in other states
11549a011532Smatthias.ringwald                 break;
11559a011532Smatthias.ringwald         }
11569a011532Smatthias.ringwald         return;
11579a011532Smatthias.ringwald     }
11589a011532Smatthias.ringwald 
115956081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
11601e6aba47Smatthias.ringwald     switch (channel->state) {
11611e6aba47Smatthias.ringwald 
11621e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
11631e6aba47Smatthias.ringwald             switch (code){
11641e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
11655932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1166f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
116738e5900eSmatthias.ringwald                     switch (result) {
116838e5900eSmatthias.ringwald                         case 0:
1169169f8b28Smatthias.ringwald                             // successful connection
1170f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1171fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
117228ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
117338e5900eSmatthias.ringwald                             break;
117438e5900eSmatthias.ringwald                         case 1:
11755932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
11765932bd7cS[email protected]                             l2cap_start_ertx(channel);
117738e5900eSmatthias.ringwald                             break;
117838e5900eSmatthias.ringwald                         default:
1179eb920dbeSmatthias.ringwald                             // channel closed
1180eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1181f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
118238e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1183eb920dbeSmatthias.ringwald 
1184eb920dbeSmatthias.ringwald                             // drop link key if security block
1185eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
11862e77e513S[email protected]                                 hci_drop_link_key_for_bd_addr(channel->address);
1187eb920dbeSmatthias.ringwald                             }
1188eb920dbeSmatthias.ringwald 
1189eb920dbeSmatthias.ringwald                             // discard channel
1190665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1191d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
119238e5900eSmatthias.ringwald                             break;
11931e6aba47Smatthias.ringwald                     }
11941e6aba47Smatthias.ringwald                     break;
119538e5900eSmatthias.ringwald 
119638e5900eSmatthias.ringwald                 default:
11971e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
119838e5900eSmatthias.ringwald                     break;
11991e6aba47Smatthias.ringwald             }
12001e6aba47Smatthias.ringwald             break;
12011e6aba47Smatthias.ringwald 
1202fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1203f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1204ae280e73Smatthias.ringwald             switch (code) {
1205ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
120628ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1207ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
120863a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
120963a7246aSmatthias.ringwald                         // only done if continuation not set
121063a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
121163a7246aSmatthias.ringwald                     }
1212ae280e73Smatthias.ringwald                     break;
12131e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12145932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12155932bd7cS[email protected]                     switch (result){
12165932bd7cS[email protected]                         case 0: // success
12175932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12185932bd7cS[email protected]                             break;
12195932bd7cS[email protected]                         case 4: // pending
12205932bd7cS[email protected]                             l2cap_start_ertx(channel);
12215932bd7cS[email protected]                             break;
12225932bd7cS[email protected]                         default:
1223fe9d8984S[email protected]                             // retry on negative result
1224fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1225fe9d8984S[email protected]                             break;
1226fe9d8984S[email protected]                     }
12275a67bd4aSmatthias.ringwald                     break;
12285a67bd4aSmatthias.ringwald                 default:
12295a67bd4aSmatthias.ringwald                     break;
12301e6aba47Smatthias.ringwald             }
1231fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1232fa8473a4Smatthias.ringwald                 // for open:
12335a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1234fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1235c8e4258aSmatthias.ringwald             }
1236c8e4258aSmatthias.ringwald             break;
1237f62db1e3Smatthias.ringwald 
1238f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1239f62db1e3Smatthias.ringwald             switch (code) {
1240f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
124127a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
124227a923d0Smatthias.ringwald                     break;
12435a67bd4aSmatthias.ringwald                 default:
12445a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
12455a67bd4aSmatthias.ringwald                     break;
124627a923d0Smatthias.ringwald             }
124727a923d0Smatthias.ringwald             break;
124884836b65Smatthias.ringwald 
124984836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
125084836b65Smatthias.ringwald             // @TODO handle incoming requests
125184836b65Smatthias.ringwald             break;
125284836b65Smatthias.ringwald 
125384836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
125484836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
125584836b65Smatthias.ringwald             break;
125610642e45Smatthias.ringwald         default:
125710642e45Smatthias.ringwald             break;
125827a923d0Smatthias.ringwald     }
12599da54300S[email protected]     // log_info("new state %u", channel->state);
126027a923d0Smatthias.ringwald }
126127a923d0Smatthias.ringwald 
126200d93d79Smatthias.ringwald 
12637f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
126400d93d79Smatthias.ringwald 
126500d93d79Smatthias.ringwald     // get code, signalind identifier and command len
126600d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
126700d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
126800d93d79Smatthias.ringwald 
126900d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
127000d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
127163a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
127200d93d79Smatthias.ringwald         return;
127300d93d79Smatthias.ringwald     }
127400d93d79Smatthias.ringwald 
127500d93d79Smatthias.ringwald     // general commands without an assigned channel
127600d93d79Smatthias.ringwald     switch(code) {
127700d93d79Smatthias.ringwald 
127800d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1279f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1280f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
128100d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
12822b83fb7dSmatthias.ringwald             return;
128300d93d79Smatthias.ringwald         }
128400d93d79Smatthias.ringwald 
12852b360848Smatthias.ringwald         case ECHO_REQUEST:
12862b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
12872b83fb7dSmatthias.ringwald             return;
128800d93d79Smatthias.ringwald 
128900d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1290f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
12912b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
12922b83fb7dSmatthias.ringwald             return;
129300d93d79Smatthias.ringwald         }
129400d93d79Smatthias.ringwald 
129500d93d79Smatthias.ringwald         default:
129600d93d79Smatthias.ringwald             break;
129700d93d79Smatthias.ringwald     }
129800d93d79Smatthias.ringwald 
129900d93d79Smatthias.ringwald 
130000d93d79Smatthias.ringwald     // Get potential destination CID
1301f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
130200d93d79Smatthias.ringwald 
130300d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1304665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1305665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1306665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1307665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1308c22aecc9S[email protected]         if (channel->handle != handle) continue;
130900d93d79Smatthias.ringwald         if (code & 1) {
1310b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1311b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
131200d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13134e32727eSmatthias.ringwald                 break;
131400d93d79Smatthias.ringwald             }
131500d93d79Smatthias.ringwald         } else {
1316b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
131700d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
131800d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13194e32727eSmatthias.ringwald                 break;
132000d93d79Smatthias.ringwald             }
132100d93d79Smatthias.ringwald         }
132200d93d79Smatthias.ringwald     }
132300d93d79Smatthias.ringwald }
132400d93d79Smatthias.ringwald 
13257f02f414SMatthias Ringwald static void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
132600d93d79Smatthias.ringwald 
132700d93d79Smatthias.ringwald     // Get Channel ID
132800d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
132900d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
133000d93d79Smatthias.ringwald 
13315652b5ffS[email protected]     switch (channel_id) {
13325652b5ffS[email protected] 
13335652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13345652b5ffS[email protected] 
133500d93d79Smatthias.ringwald             uint16_t command_offset = 8;
133600d93d79Smatthias.ringwald             while (command_offset < size) {
133700d93d79Smatthias.ringwald 
133800d93d79Smatthias.ringwald                 // handle signaling commands
133900d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
134000d93d79Smatthias.ringwald 
134100d93d79Smatthias.ringwald                 // increment command_offset
1342f8fbdce0SMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
134300d93d79Smatthias.ringwald             }
13445652b5ffS[email protected]             break;
134500d93d79Smatthias.ringwald         }
134600d93d79Smatthias.ringwald 
13475652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
13485652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
13495652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13505652b5ffS[email protected]             }
13515652b5ffS[email protected]             break;
13525652b5ffS[email protected] 
13535652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
13545652b5ffS[email protected]             if (security_protocol_packet_handler) {
13555652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13565652b5ffS[email protected]             }
13575652b5ffS[email protected]             break;
13585652b5ffS[email protected] 
1359462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1360462e630dS[email protected]             if (connectionless_channel_packet_handler) {
1361462e630dS[email protected]                 (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1362462e630dS[email protected]             }
1363462e630dS[email protected]             break;
1364462e630dS[email protected] 
13651bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1366ccf076adS[email protected]             switch (packet[8]){
1367ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1368f8fbdce0SMatthias Ringwald                     uint16_t result = little_endian_read_16(packet, 12);
1369ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1370ccf076adS[email protected]                     break;
1371ccf076adS[email protected]                 }
1372ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1373ccf076adS[email protected]                     uint8_t event[10];
1374ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1375ccf076adS[email protected]                     event[1] = 8;
1376ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1377da886c03S[email protected] 
1378da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1379da886c03S[email protected]                     if (connection){
13806d91fb6cSMatthias Ringwald                         if (connection->role != HCI_ROLE_MASTER){
13816d91fb6cSMatthias Ringwald                             // reject command without notifying upper layer when not in master role
13826d91fb6cSMatthias Ringwald                             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
13836d91fb6cSMatthias Ringwald                             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
13846d91fb6cSMatthias Ringwald                             break;
13856d91fb6cSMatthias Ringwald                         }
1386da886c03S[email protected]                         int update_parameter = 1;
1387a4c06b28SMatthias Ringwald                         le_connection_parameter_range_t existing_range;
1388a4c06b28SMatthias Ringwald                         gap_le_get_connection_parameter_range(existing_range);
1389f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_min = little_endian_read_16(packet,12);
1390f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_max = little_endian_read_16(packet,14);
1391f8fbdce0SMatthias Ringwald                         uint16_t le_conn_latency = little_endian_read_16(packet,16);
1392f8fbdce0SMatthias Ringwald                         uint16_t le_supervision_timeout = little_endian_read_16(packet,18);
1393da886c03S[email protected] 
1394da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1395da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1396da886c03S[email protected] 
1397da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1398da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1399da886c03S[email protected] 
1400da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1401da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1402da886c03S[email protected] 
1403da886c03S[email protected]                         if (update_parameter){
1404da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1405da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1406da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1407da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1408da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1409da886c03S[email protected]                         } else {
1410da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1411da886c03S[email protected]                         }
141279f53f1dSMatthias Ringwald                         connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1];
1413da886c03S[email protected]                     }
1414da886c03S[email protected] 
1415ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1416ffbf8201SMatthias Ringwald                     (*packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1417da886c03S[email protected] 
1418ccf076adS[email protected]                     break;
1419ccf076adS[email protected]                 }
1420ccf076adS[email protected]                 default: {
14211bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14221bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14231bbc0b23S[email protected]                     break;
14241bbc0b23S[email protected]                 }
1425ccf076adS[email protected]             }
1426ccf076adS[email protected]             break;
1427ccf076adS[email protected]         }
14281bbc0b23S[email protected] 
14295652b5ffS[email protected]         default: {
143000d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
143100d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
143200d93d79Smatthias.ringwald             if (channel) {
143358de5610Smatthias.ringwald                 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
143400d93d79Smatthias.ringwald             }
14355652b5ffS[email protected]             break;
14365652b5ffS[email protected]         }
14375652b5ffS[email protected]     }
143800d93d79Smatthias.ringwald }
143900d93d79Smatthias.ringwald 
14402718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
14412718e2e7Smatthias.ringwald     switch (packet_type) {
14422718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
14432718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
14442718e2e7Smatthias.ringwald             break;
14452718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
14462718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
14472718e2e7Smatthias.ringwald             break;
14482718e2e7Smatthias.ringwald         default:
14492718e2e7Smatthias.ringwald             break;
14502718e2e7Smatthias.ringwald     }
14511eb2563eS[email protected]     l2cap_run();
14522718e2e7Smatthias.ringwald }
145300d93d79Smatthias.ringwald 
145415ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
145527a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1456f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1457f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1458f62db1e3Smatthias.ringwald     // discard channel
14599dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1460665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1461d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1462c8e4258aSmatthias.ringwald }
14631e6aba47Smatthias.ringwald 
14648f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1465665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1466665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1467665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1468665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
14699d9bbc01Smatthias.ringwald         if ( service->psm == psm){
14709d9bbc01Smatthias.ringwald             return service;
14719d9bbc01Smatthias.ringwald         };
14729d9bbc01Smatthias.ringwald     }
14739d9bbc01Smatthias.ringwald     return NULL;
14749d9bbc01Smatthias.ringwald }
14759d9bbc01Smatthias.ringwald 
14767192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
14777192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
14787192e786SMatthias Ringwald }
14797192e786SMatthias Ringwald 
1480e0abb8e7S[email protected] 
1481be2053a6SMatthias 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){
1482be2053a6SMatthias Ringwald 
1483be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1484e0abb8e7S[email protected] 
14854bb582b6Smatthias.ringwald     // check for alread registered psm
14864bb582b6Smatthias.ringwald     // TODO: emit error event
14879d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1488277abc2cSmatthias.ringwald     if (service) {
1489be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1490be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1491277abc2cSmatthias.ringwald     }
14929d9bbc01Smatthias.ringwald 
14934bb582b6Smatthias.ringwald     // alloc structure
14944bb582b6Smatthias.ringwald     // TODO: emit error event
1495bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1496277abc2cSmatthias.ringwald     if (!service) {
1497be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1498be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1499277abc2cSmatthias.ringwald     }
15009d9bbc01Smatthias.ringwald 
15019d9bbc01Smatthias.ringwald     // fill in
15029d9bbc01Smatthias.ringwald     service->psm = psm;
15039d9bbc01Smatthias.ringwald     service->mtu = mtu;
150405ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1505df3354fcS[email protected]     service->required_security_level = security_level;
15069d9bbc01Smatthias.ringwald 
15079d9bbc01Smatthias.ringwald     // add to services list
1508665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1509c0e866bfSmatthias.ringwald 
1510c0e866bfSmatthias.ringwald     // enable page scan
1511c0e866bfSmatthias.ringwald     hci_connectable_control(1);
15125842b6d9Smatthias.ringwald 
1513be2053a6SMatthias Ringwald     return 0;
15149d9bbc01Smatthias.ringwald }
15159d9bbc01Smatthias.ringwald 
151602f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){
1517e0abb8e7S[email protected] 
1518e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1519e0abb8e7S[email protected] 
15209d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1521037d6e48Smatthias.ringwald     if (!service) return;
1522665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1523d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1524c0e866bfSmatthias.ringwald 
1525c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1526665d90f2SMatthias Ringwald     if (!btstack_linked_list_empty(&l2cap_services)) return;
1527c0e866bfSmatthias.ringwald     hci_connectable_control(0);
15289d9bbc01Smatthias.ringwald }
15299d9bbc01Smatthias.ringwald 
15305652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
153105ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
15325652b5ffS[email protected]     switch(channel_id){
15335652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
153405ae8de3SMatthias Ringwald             attribute_protocol_packet_handler = the_packet_handler;
15355652b5ffS[email protected]             break;
15365652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
153705ae8de3SMatthias Ringwald             security_protocol_packet_handler = the_packet_handler;
15385652b5ffS[email protected]             break;
1539462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
154005ae8de3SMatthias Ringwald             connectionless_channel_packet_handler = the_packet_handler;
1541462e630dS[email protected]             break;
15425652b5ffS[email protected]     }
15435652b5ffS[email protected] }
15445652b5ffS[email protected] 
1545a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
15467192e786SMatthias Ringwald 
15477f02f414SMatthias Ringwald 
15487f02f414SMatthias Ringwald #if 0
15497192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
15507192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
15517192e786SMatthias Ringwald }
15527192e786SMatthias Ringwald /**
15537192e786SMatthias Ringwald  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
15547192e786SMatthias Ringwald  * @param
15557192e786SMatthias Ringwald  */
1556ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm,
15577192e786SMatthias Ringwald     uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){
15587192e786SMatthias Ringwald 
15597192e786SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection);
15607192e786SMatthias Ringwald 
15617192e786SMatthias Ringwald     // check for alread registered psm
15627192e786SMatthias Ringwald     // TODO: emit error event
15637192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
15647192e786SMatthias Ringwald     if (service) {
15657192e786SMatthias Ringwald         log_error("l2cap_le_register_service_internal: PSM %u already registered", psm);
15667192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
15677192e786SMatthias Ringwald         return;
15687192e786SMatthias Ringwald     }
15697192e786SMatthias Ringwald 
15707192e786SMatthias Ringwald     // alloc structure
15717192e786SMatthias Ringwald     // TODO: emit error event
15727192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
15737192e786SMatthias Ringwald     if (!service) {
15747192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
15757192e786SMatthias Ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
15767192e786SMatthias Ringwald         return;
15777192e786SMatthias Ringwald     }
15787192e786SMatthias Ringwald 
15797192e786SMatthias Ringwald     // fill in
15807192e786SMatthias Ringwald     service->psm = psm;
15817192e786SMatthias Ringwald     service->mtu = mtu;
15827192e786SMatthias Ringwald     service->mps = mps;
15837192e786SMatthias Ringwald     service->packet_handler = packet_handler;
15847192e786SMatthias Ringwald     service->required_security_level = security_level;
15857192e786SMatthias Ringwald 
15867192e786SMatthias Ringwald     // add to services list
1587665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
15887192e786SMatthias Ringwald 
15897192e786SMatthias Ringwald     // done
15907192e786SMatthias Ringwald     l2cap_emit_service_registered(connection, 0, psm);
15917192e786SMatthias Ringwald }
15927192e786SMatthias Ringwald 
1593ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) {
15947192e786SMatthias Ringwald 
15957192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
15967192e786SMatthias Ringwald 
15977192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
15987192e786SMatthias Ringwald     if (!service) return;
1599665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
16007192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
16017192e786SMatthias Ringwald }
16027192e786SMatthias Ringwald #endif
16037f02f414SMatthias Ringwald #endif
1604