xref: /btstack/src/l2cap.c (revision e6f51008ec34e9bd51b0832cdb1a083cb0173362)
143625864Smatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
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  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
3843625864Smatthias.ringwald  *  l2cap.c
3943625864Smatthias.ringwald  *
4043625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
4143625864Smatthias.ringwald  *
4243625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
4343625864Smatthias.ringwald  */
4443625864Smatthias.ringwald 
4543625864Smatthias.ringwald #include "l2cap.h"
46645658c9Smatthias.ringwald #include "hci.h"
472b3c6c9bSmatthias.ringwald #include "hci_dump.h"
486218e6f1Smatthias.ringwald #include "debug.h"
49d3a9df87Smatthias.ringwald #include "btstack_memory.h"
5043625864Smatthias.ringwald 
5143625864Smatthias.ringwald #include <stdarg.h>
5243625864Smatthias.ringwald #include <string.h>
5343625864Smatthias.ringwald 
5443625864Smatthias.ringwald #include <stdio.h>
5543625864Smatthias.ringwald 
564c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
574c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
584c744e21Smatthias.ringwald 
5939bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
60e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6139bda6d5Smatthias.ringwald 
6200d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6700d93d79Smatthias.ringwald 
6839bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
6939bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
7039bda6d5Smatthias.ringwald 
7139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
722b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
732b83fb7dSmatthias.ringwald static int signaling_responses_pending;
742b83fb7dSmatthias.ringwald 
75c42e2ff2S[email protected] static linked_list_t l2cap_channels;
76c42e2ff2S[email protected] static linked_list_t l2cap_services;
7736944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
78808a48abSmatthias.ringwald static int new_credits_blocked = 0;
791e6aba47Smatthias.ringwald 
80c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler;
81c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler;
82ac301f95S[email protected] static uint8_t require_security_level2_for_outgoing_sdp;
835652b5ffS[email protected] 
8439bda6d5Smatthias.ringwald // prototypes
85fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
86fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
87fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
88fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
89fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
90fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
9139bda6d5Smatthias.ringwald 
9239bda6d5Smatthias.ringwald 
931e6aba47Smatthias.ringwald void l2cap_init(){
94808a48abSmatthias.ringwald     new_credits_blocked = 0;
952b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
96808a48abSmatthias.ringwald 
97f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
98f5454fc6Smatthias.ringwald     l2cap_services = NULL;
99f5454fc6Smatthias.ringwald 
100f5454fc6Smatthias.ringwald     packet_handler = null_packet_handler;
101c42e2ff2S[email protected]     attribute_protocol_packet_handler = NULL;
102c42e2ff2S[email protected]     security_protocol_packet_handler = NULL;
103f5454fc6Smatthias.ringwald 
104ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
105ac301f95S[email protected] 
106fcadd0caSmatthias.ringwald     //
1072718e2e7Smatthias.ringwald     // register callback with HCI
108fcadd0caSmatthias.ringwald     //
1092718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
110c0e866bfSmatthias.ringwald     hci_connectable_control(0); // no services yet
111fcadd0caSmatthias.ringwald }
112fcadd0caSmatthias.ringwald 
113fcadd0caSmatthias.ringwald 
114fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
11536944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
116fcadd0caSmatthias.ringwald }
11736944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
118b502e1b0Smatthias.ringwald     packet_handler = handler;
1191e6aba47Smatthias.ringwald }
1201e6aba47Smatthias.ringwald 
12158de5610Smatthias.ringwald //  notify client/protocol handler
12258de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
12358de5610Smatthias.ringwald     if (channel->packet_handler) {
12458de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
12558de5610Smatthias.ringwald     } else {
12636944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
12758de5610Smatthias.ringwald     }
12858de5610Smatthias.ringwald }
12958de5610Smatthias.ringwald 
13058de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
131c9dc710bS[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",
132e0abb8e7S[email protected]              status, bd_addr_to_str(channel->address), channel->handle, channel->psm,
133c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
134c9dc710bS[email protected]     uint8_t event[23];
13558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
13658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13758de5610Smatthias.ringwald     event[2] = status;
13858de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
13958de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
14058de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
14158de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
14258de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1434c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1444c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
14554ed2ecaS[email protected]     bt_store_16(event, 21, channel->flush_timeout);
14658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
14758de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
14858de5610Smatthias.ringwald }
14958de5610Smatthias.ringwald 
15058de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
151e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
15258de5610Smatthias.ringwald     uint8_t event[4];
15358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
15458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
15558de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
15658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15758de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
15858de5610Smatthias.ringwald }
15958de5610Smatthias.ringwald 
16058de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
161e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
162e0abb8e7S[email protected]              bd_addr_to_str(channel->address), channel->handle,  channel->psm, channel->local_cid, channel->remote_cid);
16358de5610Smatthias.ringwald     uint8_t event[16];
16458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
16558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
16658de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
16758de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
16858de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
16958de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
17058de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
17158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1730af41d30Smatthias.ringwald }
174808a48abSmatthias.ringwald 
175ccf076adS[email protected] void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){
176ccf076adS[email protected]     uint8_t event[6];
177ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
178ccf076adS[email protected]     event[1] = 4;
179ccf076adS[email protected]     bt_store_16(event, 2, handle);
180ccf076adS[email protected]     bt_store_16(event, 4, result);
181ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
182ccf076adS[email protected]     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event));
183ccf076adS[email protected] }
184ccf076adS[email protected] 
1855842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
186e0abb8e7S[email protected]     log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm);
1875842b6d9Smatthias.ringwald     uint8_t event[5];
1885842b6d9Smatthias.ringwald     event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
1895842b6d9Smatthias.ringwald     event[1] = sizeof(event) - 2;
1905842b6d9Smatthias.ringwald     event[2] = status;
1915842b6d9Smatthias.ringwald     bt_store_16(event, 3, psm);
1925842b6d9Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
193*e6f51008S[email protected]     (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
1945842b6d9Smatthias.ringwald }
1955842b6d9Smatthias.ringwald 
1966218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
197e0abb8e7S[email protected] 
198e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits);
1996218e6f1Smatthias.ringwald     // track credits
2006218e6f1Smatthias.ringwald     channel->packets_granted += credits;
2016218e6f1Smatthias.ringwald 
2026218e6f1Smatthias.ringwald     uint8_t event[5];
2036218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
2046218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
2056218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
2066218e6f1Smatthias.ringwald     event[4] = credits;
2076218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
2086218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
2096218e6f1Smatthias.ringwald }
2106218e6f1Smatthias.ringwald 
211808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
212808a48abSmatthias.ringwald     new_credits_blocked = blocked;
213808a48abSmatthias.ringwald }
214808a48abSmatthias.ringwald 
21540d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
216808a48abSmatthias.ringwald 
217808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
218808a48abSmatthias.ringwald 
219c22aecc9S[email protected]     linked_list_iterator_t it;
220c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
221c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
222c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
223e79abdd6S[email protected]         if (!hci_number_free_acl_slots_for_handle(channel->handle)) return;
2240e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2254c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2268d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2278d371091Smatthias.ringwald         }
2288d371091Smatthias.ringwald     }
2298d371091Smatthias.ringwald }
2308d371091Smatthias.ringwald 
231b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
232c22aecc9S[email protected]     linked_list_iterator_t it;
233c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
234c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
235c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
236b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
237f62db1e3Smatthias.ringwald             return channel;
238f62db1e3Smatthias.ringwald         }
239f62db1e3Smatthias.ringwald     }
240f62db1e3Smatthias.ringwald     return NULL;
241f62db1e3Smatthias.ringwald }
242f62db1e3Smatthias.ringwald 
2436b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2446b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2456b1fde37Smatthias.ringwald     if (!channel) return 0;
2466b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
247a35252c8S[email protected]     return hci_can_send_acl_packet_now(channel->handle);
2487856fb31S[email protected] }
2497856fb31S[email protected] 
2506cd4da6bS[email protected] // @deprecated
2513cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){
252a35252c8S[email protected]     // TODO provide real handle
2536cd4da6bS[email protected]     return l2cap_can_send_fixed_channel_packet_now(0x1234);
2546cd4da6bS[email protected] }
2556cd4da6bS[email protected] 
2566cd4da6bS[email protected] int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle){
2576cd4da6bS[email protected]     return hci_can_send_acl_packet_now(handle);
2583cab4fcaS[email protected] }
2593cab4fcaS[email protected] 
26096cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
26196cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
26296cbd662Smatthias.ringwald     if (channel) {
26396cbd662Smatthias.ringwald         return channel->remote_mtu;
26496cbd662Smatthias.ringwald     }
26596cbd662Smatthias.ringwald     return 0;
26696cbd662Smatthias.ringwald }
26796cbd662Smatthias.ringwald 
2685932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){
269c22aecc9S[email protected]     linked_list_iterator_t it;
270c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
271c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
272c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
2735932bd7cS[email protected]         if ( &channel->rtx == ts) {
2745932bd7cS[email protected]             return channel;
2755932bd7cS[email protected]         }
2765932bd7cS[email protected]     }
2775932bd7cS[email protected]     return NULL;
2785932bd7cS[email protected] }
2795932bd7cS[email protected] 
2805932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){
2815932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2825932bd7cS[email protected]     if (!ts) return;
2835932bd7cS[email protected] 
2845932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2855932bd7cS[email protected] 
2865932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
2875932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
2885932bd7cS[email protected]     // notify client
2895932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
2905932bd7cS[email protected] 
2915932bd7cS[email protected]     // discard channel
2929dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
2935932bd7cS[email protected]     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
2945932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
2955932bd7cS[email protected] }
2965932bd7cS[email protected] 
2975932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
2985932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
2995932bd7cS[email protected]     run_loop_remove_timer(&channel->rtx);
3005932bd7cS[email protected] }
3015932bd7cS[email protected] 
3025932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3035932bd7cS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
3045932bd7cS[email protected]     l2cap_stop_rtx(channel);
3055932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
3065932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
3075932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
3085932bd7cS[email protected] }
3095932bd7cS[email protected] 
3105932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3115932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3125932bd7cS[email protected]     l2cap_stop_rtx(channel);
3135932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
3145932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
3155932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
3165932bd7cS[email protected] }
3175932bd7cS[email protected] 
318ac301f95S[email protected] void l2cap_require_security_level_2_for_outgoing_sdp(){
319ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
320ac301f95S[email protected] }
321ac301f95S[email protected] 
322df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
323ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
324df3354fcS[email protected] }
3255932bd7cS[email protected] 
32658de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
327b1d43497Smatthias.ringwald 
328a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3299da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
330b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
331b1d43497Smatthias.ringwald     }
332b1d43497Smatthias.ringwald 
3339da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3342a373862S[email protected]     hci_reserve_packet_buffer();
335facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
33658de5610Smatthias.ringwald     va_list argptr;
33758de5610Smatthias.ringwald     va_start(argptr, identifier);
33870efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
33958de5610Smatthias.ringwald     va_end(argptr);
3409da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
341826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
34258de5610Smatthias.ringwald }
34358de5610Smatthias.ringwald 
34470efece1S[email protected] #ifdef HAVE_BLE
34570efece1S[email protected] int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
34670efece1S[email protected] 
347a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3489da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
34970efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
35070efece1S[email protected]     }
35170efece1S[email protected] 
3529da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3532a373862S[email protected]     hci_reserve_packet_buffer();
354facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
35570efece1S[email protected]     va_list argptr;
35670efece1S[email protected]     va_start(argptr, identifier);
35770efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
35870efece1S[email protected]     va_end(argptr);
3599da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
360826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
36170efece1S[email protected] }
36270efece1S[email protected] #endif
36370efece1S[email protected] 
364b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
365facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
366b1d43497Smatthias.ringwald }
3676218e6f1Smatthias.ringwald 
3686b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3696b4af23dS[email protected]     return hci_reserve_packet_buffer();
3706b4af23dS[email protected] }
3716b4af23dS[email protected] 
37268a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
37368a0fcf7S[email protected]     hci_release_packet_buffer();
37468a0fcf7S[email protected] }
37568a0fcf7S[email protected] 
37668a0fcf7S[email protected] 
377b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
378b1d43497Smatthias.ringwald 
379c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
380c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
381c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
382c8b9416aS[email protected]     }
383c8b9416aS[email protected] 
38458de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
385b1d43497Smatthias.ringwald     if (!channel) {
3869da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
387b1d43497Smatthias.ringwald         return -1;   // TODO: define error
3886218e6f1Smatthias.ringwald     }
3896218e6f1Smatthias.ringwald 
390b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
3919da54300S[email protected]         log_error("l2cap_send_prepared cid 0x%02x, no credits!", local_cid);
392b1d43497Smatthias.ringwald         return -1;  // TODO: define error
393b1d43497Smatthias.ringwald     }
394b1d43497Smatthias.ringwald 
395a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(channel->handle)){
3969da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
397a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
398a35252c8S[email protected]     }
399a35252c8S[email protected] 
400b1d43497Smatthias.ringwald     --channel->packets_granted;
401b1d43497Smatthias.ringwald 
4029da54300S[email protected]     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used, credits left %u;",
403b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
404b1d43497Smatthias.ringwald 
405facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
406b1d43497Smatthias.ringwald 
407e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
408e9772277S[email protected] 
409e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
410e9772277S[email protected]     bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14));
41158de5610Smatthias.ringwald     // 2 - ACL length
41258de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
41358de5610Smatthias.ringwald     // 4 - L2CAP packet length
41458de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
41558de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
41658de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
41758de5610Smatthias.ringwald     // send
418826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
41991b99603Smatthias.ringwald 
42091b99603Smatthias.ringwald     l2cap_hand_out_credits();
42191b99603Smatthias.ringwald 
4226218e6f1Smatthias.ringwald     return err;
42358de5610Smatthias.ringwald }
42458de5610Smatthias.ringwald 
4252149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
4262149f12eSmatthias.ringwald 
427c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
428c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4292149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4302149f12eSmatthias.ringwald     }
4312149f12eSmatthias.ringwald 
432a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(handle)){
4339da54300S[email protected]         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid);
434c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
435c8b9416aS[email protected]     }
436c8b9416aS[email protected] 
4379da54300S[email protected]     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid);
4382149f12eSmatthias.ringwald 
439facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4402149f12eSmatthias.ringwald 
441e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
442e9772277S[email protected] 
443e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
444e9772277S[email protected]     bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
4452149f12eSmatthias.ringwald     // 2 - ACL length
4462149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
4472149f12eSmatthias.ringwald     // 4 - L2CAP packet length
4482149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
4492149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
4502149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 6, cid);
4512149f12eSmatthias.ringwald     // send
452826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4532149f12eSmatthias.ringwald 
4542149f12eSmatthias.ringwald     l2cap_hand_out_credits();
4552149f12eSmatthias.ringwald 
4562149f12eSmatthias.ringwald     return err;
4572149f12eSmatthias.ringwald }
4582149f12eSmatthias.ringwald 
459b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
460b1d43497Smatthias.ringwald 
461a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
462a35252c8S[email protected]     if (!channel) {
4639da54300S[email protected]         log_error("l2cap_send_internal no channel for cid 0x%02x", local_cid);
464a35252c8S[email protected]         return -1;   // TODO: define error
465a35252c8S[email protected]     }
466a35252c8S[email protected] 
467a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(channel->handle)){
4689da54300S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send", local_cid);
469b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
470b1d43497Smatthias.ringwald     }
471b1d43497Smatthias.ringwald 
4722a373862S[email protected]     hci_reserve_packet_buffer();
473facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
474b1d43497Smatthias.ringwald 
475b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
476b1d43497Smatthias.ringwald 
477b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
478b1d43497Smatthias.ringwald }
479b1d43497Smatthias.ringwald 
4802149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
4812149f12eSmatthias.ringwald 
482a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4839da54300S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send", cid);
4842149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4852149f12eSmatthias.ringwald     }
4862149f12eSmatthias.ringwald 
4872a373862S[email protected]     hci_reserve_packet_buffer();
488facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4892149f12eSmatthias.ringwald 
4902149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4912149f12eSmatthias.ringwald 
4922149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4932149f12eSmatthias.ringwald }
4942149f12eSmatthias.ringwald 
4950e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){
4960e37e417S[email protected]     return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data);
4970e37e417S[email protected] }
4980e37e417S[email protected] 
49928ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
50028ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
50128ca2b46S[email protected] }
50228ca2b46S[email protected] 
50328ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
50428ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
50528ca2b46S[email protected] }
50628ca2b46S[email protected] 
50728ca2b46S[email protected] 
508b1d43497Smatthias.ringwald 
5098158c421Smatthias.ringwald // MARK: L2CAP_RUN
5102cd0be45Smatthias.ringwald // process outstanding signaling tasks
5112cd0be45Smatthias.ringwald void l2cap_run(void){
5122b83fb7dSmatthias.ringwald 
5132b83fb7dSmatthias.ringwald     // check pending signaling responses
5142b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5152b83fb7dSmatthias.ringwald 
5162b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
517a35252c8S[email protected] 
518a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
519a35252c8S[email protected] 
5202b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5212b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
52263a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
5232b83fb7dSmatthias.ringwald 
5242b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
5252b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5262b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5272bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5282bd8b7e7S[email protected]                 hci_disconnect_security_block(handle);
5292b360848Smatthias.ringwald                 break;
5302b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5312b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5322b83fb7dSmatthias.ringwald                 break;
5332b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5343b0484b3S[email protected]                 switch (infoType){
5353b0484b3S[email protected]                     case 1: { // Connectionless MTU
5363b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5373b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5383b0484b3S[email protected]                         break;
5393b0484b3S[email protected]                     }
5403b0484b3S[email protected]                     case 2: { // Extended Features Supported
5413b0484b3S[email protected]                         // extended features request supported, only supporing fixed channel map
5423b0484b3S[email protected]                         uint32_t features = 0x80;
5433b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5443b0484b3S[email protected]                         break;
5453b0484b3S[email protected]                     }
5463b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5473b0484b3S[email protected]                         uint8_t map[8];
5483b0484b3S[email protected]                         memset(map, 0, 8);
5493b0484b3S[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel
5503b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5513b0484b3S[email protected]                         break;
5523b0484b3S[email protected]                     }
5533b0484b3S[email protected]                     default:
5542b83fb7dSmatthias.ringwald                         // all other types are not supported
5552b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5563b0484b3S[email protected]                         break;
5572b83fb7dSmatthias.ringwald                 }
5582b83fb7dSmatthias.ringwald                 break;
55963a7246aSmatthias.ringwald             case COMMAND_REJECT:
5605ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
56170efece1S[email protected] #ifdef HAVE_BLE
56270efece1S[email protected]             case COMMAND_REJECT_LE:
56370efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
56463a7246aSmatthias.ringwald                 break;
56570efece1S[email protected] #endif
5662b83fb7dSmatthias.ringwald             default:
5672b83fb7dSmatthias.ringwald                 // should not happen
5682b83fb7dSmatthias.ringwald                 break;
5692b83fb7dSmatthias.ringwald         }
5702b83fb7dSmatthias.ringwald 
5712b83fb7dSmatthias.ringwald         // remove first item
5722b83fb7dSmatthias.ringwald         signaling_responses_pending--;
5732b83fb7dSmatthias.ringwald         int i;
5742b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
57576115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
5762b83fb7dSmatthias.ringwald         }
5772b83fb7dSmatthias.ringwald     }
5782b83fb7dSmatthias.ringwald 
579ae280e73Smatthias.ringwald     uint8_t  config_options[4];
580c22aecc9S[email protected]     linked_list_iterator_t it;
581c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
582c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
583baf94f06S[email protected] 
584c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
5859da54300S[email protected]         // log_info("l2cap_run: state %u, var 0x%02x", channel->state, channel->state_var);
5862cd0be45Smatthias.ringwald         switch (channel->state){
5872cd0be45Smatthias.ringwald 
588df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
589ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
590baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
591a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
592ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
593a00031e2S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
594ad671560S[email protected]                 }
595ad671560S[email protected]                 break;
596ad671560S[email protected] 
59702b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
598baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
59964472d52Smatthias.ringwald                 // send connection request - set state first
60064472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
60102b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6028f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
60302b22dc4Smatthias.ringwald                 break;
60402b22dc4Smatthias.ringwald 
605e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
606baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
6071eb2563eS[email protected]                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
608e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6099dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
610c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
611d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
612e7ff783cSmatthias.ringwald                 break;
613e7ff783cSmatthias.ringwald 
614552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
615baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
616fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
61728ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
6182a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
619552d92a1Smatthias.ringwald                 break;
620552d92a1Smatthias.ringwald 
6216fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
622baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
6236fdcc387Smatthias.ringwald                 // success, start l2cap handshake
624b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6256fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
6262a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6275932bd7cS[email protected]                 l2cap_start_rtx(channel);
6286fdcc387Smatthias.ringwald                 break;
6296fdcc387Smatthias.ringwald 
630fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
631baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
63273cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
63363a7246aSmatthias.ringwald                     uint16_t flags = 0;
63428ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
63563a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
63663a7246aSmatthias.ringwald                         flags = 1;
63763a7246aSmatthias.ringwald                     } else {
63828ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
63963a7246aSmatthias.ringwald                     }
64063a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
64163a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
64263a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
64363a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
64463a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
64563a7246aSmatthias.ringwald                         bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
64663a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
64763a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
64863a7246aSmatthias.ringwald                     } else {
64963a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
65063a7246aSmatthias.ringwald                     }
65163a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
652fa8473a4Smatthias.ringwald                 }
65373cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
65428ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
65528ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
656b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
657ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
658ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
659ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
660b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6615932bd7cS[email protected]                     l2cap_start_rtx(channel);
662fa8473a4Smatthias.ringwald                 }
663fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
664552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
665552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
666552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
667fa8473a4Smatthias.ringwald                 }
668552d92a1Smatthias.ringwald                 break;
669552d92a1Smatthias.ringwald 
670e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
671baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
672b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6735932bd7cS[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 :)
674756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
675e7ff783cSmatthias.ringwald                 break;
676e7ff783cSmatthias.ringwald 
677e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
678baf94f06S[email protected]                 if (!hci_can_send_acl_packet_now(channel->handle)) break;
679b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6802cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
6812a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
6822cd0be45Smatthias.ringwald                 break;
6832cd0be45Smatthias.ringwald             default:
6842cd0be45Smatthias.ringwald                 break;
6852cd0be45Smatthias.ringwald         }
6862cd0be45Smatthias.ringwald     }
687da886c03S[email protected] 
6884d7157c3S[email protected] #ifdef HAVE_BLE
689da886c03S[email protected]     // send l2cap con paramter update if necessary
690da886c03S[email protected]     hci_connections_get_iterator(&it);
691da886c03S[email protected]     while(linked_list_iterator_has_next(&it)){
692da886c03S[email protected]         hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it);
693da886c03S[email protected]         int result;
694da886c03S[email protected] 
695da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
696da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
697da886c03S[email protected]                 result = 0;
698da886c03S[email protected]                 break;
699da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
700da886c03S[email protected]                 result = 1;
701da886c03S[email protected]                 break;
702da886c03S[email protected]             default:
703da886c03S[email protected]                 result = -1;
704da886c03S[email protected]                 break;
705da886c03S[email protected]         }
706da886c03S[email protected]         if (result < 0) break;
707da886c03S[email protected] 
708da886c03S[email protected]         if (!hci_can_send_acl_packet_now(connection->con_handle)) break;
709da886c03S[email protected]         hci_reserve_packet_buffer();
710da886c03S[email protected]         uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
711da886c03S[email protected]         connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
712da886c03S[email protected]         uint16_t len = l2cap_le_create_connection_parameter_update_response(acl_buffer, connection->con_handle, 0);
713da886c03S[email protected]         hci_send_acl_packet_buffer(len);
714da886c03S[email protected]     }
7154d7157c3S[email protected] #endif
716da886c03S[email protected] 
7172cd0be45Smatthias.ringwald }
7182cd0be45Smatthias.ringwald 
7194aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7204ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
721fa8c92f6Smatthias.ringwald }
722fa8c92f6Smatthias.ringwald 
723eb7f386bS[email protected] uint16_t l2cap_max_le_mtu(){
7244ff786cfS[email protected]     return l2cap_max_mtu();
725e5e1518dS[email protected] }
726e5e1518dS[email protected] 
7272df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){
7282df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7295533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7302df5dadcS[email protected]         // success, start l2cap handshake
7312df5dadcS[email protected]         channel->handle = handle;
7322df5dadcS[email protected]         channel->local_cid = l2cap_next_local_cid();
7332df5dadcS[email protected]         // check remote SSP feature first
7342df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7352df5dadcS[email protected]     }
7362df5dadcS[email protected] }
7372df5dadcS[email protected] 
7382df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7392df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7402df5dadcS[email protected] 
7412df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
742ac301f95S[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));
7432df5dadcS[email protected]     if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7442df5dadcS[email protected]         // request security level 2
7452df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
7461429b2d6S[email protected]         gap_request_security_level(channel->handle, LEVEL_2);
7472df5dadcS[email protected]         return;
7482df5dadcS[email protected]     }
7492df5dadcS[email protected]     // fine, go ahead
7502df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7512df5dadcS[email protected] }
7522df5dadcS[email protected] 
7531e6aba47Smatthias.ringwald // open outgoing L2CAP channel
75415470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
75515470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
7561e6aba47Smatthias.ringwald 
757e0abb8e7S[email protected]     log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
758e0abb8e7S[email protected] 
7591e6aba47Smatthias.ringwald     // alloc structure
760bb69aaaeS[email protected]     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
7612b360848Smatthias.ringwald     if (!chan) {
7622b360848Smatthias.ringwald         // emit error event
7632b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
7642b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
7652b360848Smatthias.ringwald         dummy_channel.psm = psm;
7662b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
7672b360848Smatthias.ringwald         return;
7682b360848Smatthias.ringwald     }
7691d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
7702985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
7712985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
7729775e25bSmatthias.ringwald     }
7739775e25bSmatthias.ringwald 
7741e6aba47Smatthias.ringwald     // fill in
7751e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
7761e6aba47Smatthias.ringwald     chan->psm = psm;
7771e6aba47Smatthias.ringwald     chan->handle = 0;
7781e6aba47Smatthias.ringwald     chan->connection = connection;
7796b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
7802784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
78115470d27Smatthias.ringwald     chan->local_mtu = mtu;
7826218e6f1Smatthias.ringwald     chan->packets_granted = 0;
7836218e6f1Smatthias.ringwald 
7841e6aba47Smatthias.ringwald     // set initial state
78502b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
78673cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
787b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
788b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
7895533f01eS[email protected]     chan->required_security_level = LEVEL_0;
7901e6aba47Smatthias.ringwald 
7911e6aba47Smatthias.ringwald     // add to connections list
7921e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
7931e6aba47Smatthias.ringwald 
7942df5dadcS[email protected]     // check if hci connection is already usable
79596a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type((bd_addr_t*)address, BD_ADDR_TYPE_CLASSIC);
7962df5dadcS[email protected]     if (conn){
7975533f01eS[email protected]         log_info("l2cap_create_channel_internal, hci connection already exists");
7982df5dadcS[email protected]         l2cap_handle_connection_complete(conn->con_handle, chan);
7992df5dadcS[email protected]         // check ir remote supported fearures are already received
8002df5dadcS[email protected]         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
8012df5dadcS[email protected]             l2cap_handle_remote_supported_features_received(chan);
8022df5dadcS[email protected]         }
8032df5dadcS[email protected]     }
8042df5dadcS[email protected] 
80502b22dc4Smatthias.ringwald     l2cap_run();
80643625864Smatthias.ringwald }
80743625864Smatthias.ringwald 
808b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
809e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
810b35f641cSmatthias.ringwald     // find channel for local_cid
811b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
812f62db1e3Smatthias.ringwald     if (channel) {
813e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
814f62db1e3Smatthias.ringwald     }
8152cd0be45Smatthias.ringwald     // process
8162cd0be45Smatthias.ringwald     l2cap_run();
81743625864Smatthias.ringwald }
8181e6aba47Smatthias.ringwald 
819afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
820c22aecc9S[email protected]     linked_list_iterator_t it;
821c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
822c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
823c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
824c22aecc9S[email protected]         if ( BD_ADDR_CMP( channel->address, address) != 0) continue;
825c22aecc9S[email protected]         // channel for this address found
826c22aecc9S[email protected]         switch (channel->state){
827c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
828c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
829afde0c52Smatthias.ringwald                 // failure, forward error code
830afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
831afde0c52Smatthias.ringwald                 // discard channel
8329dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
833c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
834d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
835c22aecc9S[email protected]                 break;
836c22aecc9S[email protected]             default:
837c22aecc9S[email protected]                 break;
838afde0c52Smatthias.ringwald         }
839afde0c52Smatthias.ringwald     }
840afde0c52Smatthias.ringwald }
841afde0c52Smatthias.ringwald 
842afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
843c22aecc9S[email protected]     linked_list_iterator_t it;
844c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
845c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
846c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
847afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
8482df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
849afde0c52Smatthias.ringwald         }
850afde0c52Smatthias.ringwald     }
8516fdcc387Smatthias.ringwald     // process
8526fdcc387Smatthias.ringwald     l2cap_run();
853afde0c52Smatthias.ringwald }
854b448a0e7Smatthias.ringwald 
855afde0c52Smatthias.ringwald void l2cap_event_handler(uint8_t *packet, uint16_t size){
856afde0c52Smatthias.ringwald 
857afde0c52Smatthias.ringwald     bd_addr_t address;
858afde0c52Smatthias.ringwald     hci_con_handle_t handle;
859c22aecc9S[email protected]     linked_list_iterator_t it;
8602d00edd4Smatthias.ringwald     int hci_con_used;
861afde0c52Smatthias.ringwald 
862afde0c52Smatthias.ringwald     switch(packet[0]){
863afde0c52Smatthias.ringwald 
864afde0c52Smatthias.ringwald         // handle connection complete events
865afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
866afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
867afde0c52Smatthias.ringwald             if (packet[2] == 0){
868afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
869afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
870afde0c52Smatthias.ringwald             } else {
871afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
872afde0c52Smatthias.ringwald             }
873afde0c52Smatthias.ringwald             break;
874afde0c52Smatthias.ringwald 
875afde0c52Smatthias.ringwald         // handle successful create connection cancel command
876afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
877afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
878afde0c52Smatthias.ringwald                 if (packet[5] == 0){
879afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
880afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
881afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
88203cfbabcSmatthias.ringwald                 }
8831e6aba47Smatthias.ringwald             }
88439d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
88539d59809Smatthias.ringwald             break;
88639d59809Smatthias.ringwald 
88739d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
88839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
889afde0c52Smatthias.ringwald             break;
89027a923d0Smatthias.ringwald 
8911e6aba47Smatthias.ringwald         // handle disconnection complete events
892afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
893c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
894afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
895c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
896c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
897c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
898c22aecc9S[email protected]                 if (channel->handle != handle) continue;
89915ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
9009dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
901c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
902d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
90327a923d0Smatthias.ringwald             }
904afde0c52Smatthias.ringwald             break;
905fcadd0caSmatthias.ringwald 
9066218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
90702b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
9088d371091Smatthias.ringwald             l2cap_hand_out_credits();
9096218e6f1Smatthias.ringwald             break;
9106218e6f1Smatthias.ringwald 
911ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
912afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
91336944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
91480ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
9152d00edd4Smatthias.ringwald             hci_con_used = 0;
916c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
917c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
918c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
919c22aecc9S[email protected]                 if (channel->handle != handle) continue;
9202d00edd4Smatthias.ringwald                 hci_con_used = 1;
921c22aecc9S[email protected]                 break;
922ee091cf1Smatthias.ringwald             }
9232d00edd4Smatthias.ringwald             if (hci_con_used) break;
924d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
9259edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
926afde0c52Smatthias.ringwald             break;
927ee091cf1Smatthias.ringwald 
9286e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
929c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
930c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
931c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
932c22aecc9S[email protected]                 if (!channel->packet_handler) continue;
9336e6710ebSmatthias.ringwald                 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
9346e6710ebSmatthias.ringwald             }
9355652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
9365652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
9375652b5ffS[email protected]             }
9385652b5ffS[email protected]             if (security_protocol_packet_handler) {
939133efcfdSmatthias.ringwald                 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
9405652b5ffS[email protected]             }
9416e6710ebSmatthias.ringwald             break;
9426e6710ebSmatthias.ringwald 
943df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
944df3354fcS[email protected]             handle = READ_BT_16(packet, 3);
945c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
946c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
947c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
948df3354fcS[email protected]                 if (channel->handle != handle) continue;
9492df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
950df3354fcS[email protected]                 break;
951df3354fcS[email protected]             }
952c22aecc9S[email protected]             break;
953df3354fcS[email protected] 
954a00031e2S[email protected]         case GAP_SECURITY_LEVEL:
955a00031e2S[email protected]             handle = READ_BT_16(packet, 2);
956bd63148eS[email protected]             log_info("l2cap - security level update");
957c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
958c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
959c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
960f85a9399S[email protected]                 if (channel->handle != handle) continue;
9615533f01eS[email protected] 
962bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
963bd63148eS[email protected] 
9645533f01eS[email protected]                 gap_security_level_t actual_level = packet[4];
9655533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
9665533f01eS[email protected] 
967df3354fcS[email protected]                 switch (channel->state){
968df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
9695533f01eS[email protected]                         if (actual_level >= required_level){
970f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
971f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
9721eb2563eS[email protected]                         } else {
9731eb2563eS[email protected]                             channel->reason = 0x03; // security block
9741eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
9751eb2563eS[email protected]                         }
976df3354fcS[email protected]                         break;
977df3354fcS[email protected] 
978df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
9795533f01eS[email protected]                         if (actual_level >= required_level){
980df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
981df3354fcS[email protected]                         } else {
982df3354fcS[email protected]                             // disconnnect, authentication not good enough
983df3354fcS[email protected]                             hci_disconnect_security_block(handle);
984df3354fcS[email protected]                         }
985df3354fcS[email protected]                         break;
986df3354fcS[email protected] 
987df3354fcS[email protected]                     default:
988df3354fcS[email protected]                         break;
989df3354fcS[email protected]                 }
990f85a9399S[email protected]             }
991f85a9399S[email protected]             break;
992f85a9399S[email protected] 
993afde0c52Smatthias.ringwald         default:
994afde0c52Smatthias.ringwald             break;
995afde0c52Smatthias.ringwald     }
996afde0c52Smatthias.ringwald 
997e0707417S[email protected]     // pass on: main packet handler, att and sm packet handlers
998b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
999e0707417S[email protected]     if (attribute_protocol_packet_handler){
1000e0707417S[email protected]         (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
1001e0707417S[email protected]     }
1002e0707417S[email protected]     if (security_protocol_packet_handler) {
1003e0707417S[email protected]         (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
1004e0707417S[email protected]     }
1005bd63148eS[email protected] 
1006bd63148eS[email protected]     l2cap_run();
10071e6aba47Smatthias.ringwald }
10081e6aba47Smatthias.ringwald 
1009afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1010b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1011e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1012e7ff783cSmatthias.ringwald     l2cap_run();
101384836b65Smatthias.ringwald }
101484836b65Smatthias.ringwald 
10152b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
10164cf56b4aSmatthias.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."
10172b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
10182b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
10192b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
10202b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
10212b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
10222b360848Smatthias.ringwald         signaling_responses_pending++;
10232b360848Smatthias.ringwald         l2cap_run();
10242b360848Smatthias.ringwald     }
10252b360848Smatthias.ringwald }
10262b360848Smatthias.ringwald 
1027b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1028645658c9Smatthias.ringwald 
10299da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1030645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1031645658c9Smatthias.ringwald     if (!service) {
1032645658c9Smatthias.ringwald         // 0x0002 PSM not supported
10332b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1034645658c9Smatthias.ringwald         return;
1035645658c9Smatthias.ringwald     }
1036645658c9Smatthias.ringwald 
10375061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1038645658c9Smatthias.ringwald     if (!hci_connection) {
10392b360848Smatthias.ringwald         //
10409da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1041645658c9Smatthias.ringwald         return;
1042645658c9Smatthias.ringwald     }
10432bd8b7e7S[email protected] 
10442bd8b7e7S[email protected]     // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP
1045b087afb5S[email protected]     if (  hci_ssp_supported_on_both_sides(handle)
1046b087afb5S[email protected]     &&    gap_security_level(handle) == LEVEL_0
1047b087afb5S[email protected]     &&   !l2cap_security_level_0_allowed_for_PSM(psm)){
10482bd8b7e7S[email protected] 
10492bd8b7e7S[email protected]         // 0x0003 Security Block
10502bd8b7e7S[email protected]         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003);
10512bd8b7e7S[email protected]         return;
10522bd8b7e7S[email protected]     }
10532bd8b7e7S[email protected] 
10542bd8b7e7S[email protected] 
1055645658c9Smatthias.ringwald     // alloc structure
10569da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1057bb69aaaeS[email protected]     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
10582b360848Smatthias.ringwald     if (!channel){
10592b360848Smatthias.ringwald         // 0x0004 No resources available
10602b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10612b360848Smatthias.ringwald         return;
10622b360848Smatthias.ringwald     }
1063645658c9Smatthias.ringwald 
1064645658c9Smatthias.ringwald     // fill in
1065169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
1066169f8b28Smatthias.ringwald     channel->psm = psm;
1067169f8b28Smatthias.ringwald     channel->handle = handle;
1068169f8b28Smatthias.ringwald     channel->connection = service->connection;
1069f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1070b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1071b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1072fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
10730a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1074761b0451Smatthias.ringwald     channel->packets_granted = 0;
1075b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1076df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1077645658c9Smatthias.ringwald 
10781d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
10792985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
10802985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
10819775e25bSmatthias.ringwald     }
10829775e25bSmatthias.ringwald 
1083645658c9Smatthias.ringwald     // set initial state
1084df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1085ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1086e405ae81Smatthias.ringwald 
1087645658c9Smatthias.ringwald     // add to connections list
1088169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
1089645658c9Smatthias.ringwald 
1090f85a9399S[email protected]     // assert security requirements
10911eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1092e405ae81Smatthias.ringwald }
1093645658c9Smatthias.ringwald 
1094b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
1095e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1096b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1097e405ae81Smatthias.ringwald     if (!channel) {
10987d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
1099e405ae81Smatthias.ringwald         return;
1100e405ae81Smatthias.ringwald     }
1101e405ae81Smatthias.ringwald 
1102552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1103e405ae81Smatthias.ringwald 
1104552d92a1Smatthias.ringwald     // process
1105552d92a1Smatthias.ringwald     l2cap_run();
1106e405ae81Smatthias.ringwald }
1107645658c9Smatthias.ringwald 
1108b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
1109e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1110b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1111e405ae81Smatthias.ringwald     if (!channel) {
11127d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
1113e405ae81Smatthias.ringwald         return;
1114e405ae81Smatthias.ringwald     }
1115e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1116e7ff783cSmatthias.ringwald     channel->reason = reason;
1117e7ff783cSmatthias.ringwald     l2cap_run();
1118645658c9Smatthias.ringwald }
1119645658c9Smatthias.ringwald 
11202784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1121b1988dceSmatthias.ringwald 
1122b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1123b1988dceSmatthias.ringwald 
112463a7246aSmatthias.ringwald     uint16_t flags = READ_BT_16(command, 6);
112563a7246aSmatthias.ringwald     if (flags & 1) {
112663a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
112763a7246aSmatthias.ringwald     }
112863a7246aSmatthias.ringwald 
11292784b77dSmatthias.ringwald     // accept the other's configuration options
11303de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
11313de7c0caSmatthias.ringwald     uint16_t pos     = 8;
11323de7c0caSmatthias.ringwald     while (pos < end_pos){
113363a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
113463a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
113563a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
113663a7246aSmatthias.ringwald         pos++;
11371dc511deSmatthias.ringwald         uint8_t length = command[pos++];
11381dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
113963a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
11401dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
11419da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
114263a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
114363a7246aSmatthias.ringwald         }
11440fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11450fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
11460fe7a9d0S[email protected]             channel->flush_timeout = READ_BT_16(command, pos);
11470fe7a9d0S[email protected]         }
114863a7246aSmatthias.ringwald         // check for unknown options
114963a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1150c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
115163a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11521dc511deSmatthias.ringwald         }
11531dc511deSmatthias.ringwald         pos += length;
11541dc511deSmatthias.ringwald     }
11552784b77dSmatthias.ringwald }
11562784b77dSmatthias.ringwald 
1157fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11589da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
115973cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
116073cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1161fa8473a4Smatthias.ringwald     return 1;
1162fa8473a4Smatthias.ringwald }
1163fa8473a4Smatthias.ringwald 
1164fa8473a4Smatthias.ringwald 
116500d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11661e6aba47Smatthias.ringwald 
116700d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
116800d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
116938e5900eSmatthias.ringwald     uint16_t result = 0;
11701e6aba47Smatthias.ringwald 
11719da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1172b35f641cSmatthias.ringwald 
11739a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
11749a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
11759a011532Smatthias.ringwald         switch (channel->state){
1176fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
11779a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
11782b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
11799a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
11809a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
11819a011532Smatthias.ringwald                 break;
11829a011532Smatthias.ringwald 
11839a011532Smatthias.ringwald             default:
11849a011532Smatthias.ringwald                 // ignore in other states
11859a011532Smatthias.ringwald                 break;
11869a011532Smatthias.ringwald         }
11879a011532Smatthias.ringwald         return;
11889a011532Smatthias.ringwald     }
11899a011532Smatthias.ringwald 
119056081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
11911e6aba47Smatthias.ringwald     switch (channel->state) {
11921e6aba47Smatthias.ringwald 
11931e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
11941e6aba47Smatthias.ringwald             switch (code){
11951e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
11965932bd7cS[email protected]                     l2cap_stop_rtx(channel);
119700d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
119838e5900eSmatthias.ringwald                     switch (result) {
119938e5900eSmatthias.ringwald                         case 0:
1200169f8b28Smatthias.ringwald                             // successful connection
120100d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1202fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
120328ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
120438e5900eSmatthias.ringwald                             break;
120538e5900eSmatthias.ringwald                         case 1:
12065932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
12075932bd7cS[email protected]                             l2cap_start_ertx(channel);
120838e5900eSmatthias.ringwald                             break;
120938e5900eSmatthias.ringwald                         default:
1210eb920dbeSmatthias.ringwald                             // channel closed
1211eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1212f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
121338e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1214eb920dbeSmatthias.ringwald 
1215eb920dbeSmatthias.ringwald                             // drop link key if security block
1216eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
1217eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
1218eb920dbeSmatthias.ringwald                             }
1219eb920dbeSmatthias.ringwald 
1220eb920dbeSmatthias.ringwald                             // discard channel
1221eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1222d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
122338e5900eSmatthias.ringwald                             break;
12241e6aba47Smatthias.ringwald                     }
12251e6aba47Smatthias.ringwald                     break;
122638e5900eSmatthias.ringwald 
122738e5900eSmatthias.ringwald                 default:
12281e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
122938e5900eSmatthias.ringwald                     break;
12301e6aba47Smatthias.ringwald             }
12311e6aba47Smatthias.ringwald             break;
12321e6aba47Smatthias.ringwald 
1233fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1234fe9d8984S[email protected]             result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1235ae280e73Smatthias.ringwald             switch (code) {
1236ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
123728ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1238ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
123963a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
124063a7246aSmatthias.ringwald                         // only done if continuation not set
124163a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
124263a7246aSmatthias.ringwald                     }
1243ae280e73Smatthias.ringwald                     break;
12441e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12455932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12465932bd7cS[email protected]                     switch (result){
12475932bd7cS[email protected]                         case 0: // success
12485932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12495932bd7cS[email protected]                             break;
12505932bd7cS[email protected]                         case 4: // pending
12515932bd7cS[email protected]                             l2cap_start_ertx(channel);
12525932bd7cS[email protected]                             break;
12535932bd7cS[email protected]                         default:
1254fe9d8984S[email protected]                             // retry on negative result
1255fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1256fe9d8984S[email protected]                             break;
1257fe9d8984S[email protected]                     }
12585a67bd4aSmatthias.ringwald                     break;
12595a67bd4aSmatthias.ringwald                 default:
12605a67bd4aSmatthias.ringwald                     break;
12611e6aba47Smatthias.ringwald             }
1262fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1263fa8473a4Smatthias.ringwald                 // for open:
12645a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1265fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
12666218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
1267c8e4258aSmatthias.ringwald             }
1268c8e4258aSmatthias.ringwald             break;
1269f62db1e3Smatthias.ringwald 
1270f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1271f62db1e3Smatthias.ringwald             switch (code) {
1272f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
127327a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
127427a923d0Smatthias.ringwald                     break;
12755a67bd4aSmatthias.ringwald                 default:
12765a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
12775a67bd4aSmatthias.ringwald                     break;
127827a923d0Smatthias.ringwald             }
127927a923d0Smatthias.ringwald             break;
128084836b65Smatthias.ringwald 
128184836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
128284836b65Smatthias.ringwald             // @TODO handle incoming requests
128384836b65Smatthias.ringwald             break;
128484836b65Smatthias.ringwald 
128584836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
128684836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
128784836b65Smatthias.ringwald             break;
128810642e45Smatthias.ringwald         default:
128910642e45Smatthias.ringwald             break;
129027a923d0Smatthias.ringwald     }
12919da54300S[email protected]     // log_info("new state %u", channel->state);
129227a923d0Smatthias.ringwald }
129327a923d0Smatthias.ringwald 
129400d93d79Smatthias.ringwald 
129500d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
129600d93d79Smatthias.ringwald 
129700d93d79Smatthias.ringwald     // get code, signalind identifier and command len
129800d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
129900d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
130000d93d79Smatthias.ringwald 
130100d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
130200d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
130363a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
130400d93d79Smatthias.ringwald         return;
130500d93d79Smatthias.ringwald     }
130600d93d79Smatthias.ringwald 
130700d93d79Smatthias.ringwald     // general commands without an assigned channel
130800d93d79Smatthias.ringwald     switch(code) {
130900d93d79Smatthias.ringwald 
131000d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
131100d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
131200d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
131300d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
13142b83fb7dSmatthias.ringwald             return;
131500d93d79Smatthias.ringwald         }
131600d93d79Smatthias.ringwald 
13172b360848Smatthias.ringwald         case ECHO_REQUEST:
13182b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
13192b83fb7dSmatthias.ringwald             return;
132000d93d79Smatthias.ringwald 
132100d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
132200d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
13232b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
13242b83fb7dSmatthias.ringwald             return;
132500d93d79Smatthias.ringwald         }
132600d93d79Smatthias.ringwald 
132700d93d79Smatthias.ringwald         default:
132800d93d79Smatthias.ringwald             break;
132900d93d79Smatthias.ringwald     }
133000d93d79Smatthias.ringwald 
133100d93d79Smatthias.ringwald 
133200d93d79Smatthias.ringwald     // Get potential destination CID
133300d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
133400d93d79Smatthias.ringwald 
133500d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1336c22aecc9S[email protected]     linked_list_iterator_t it;
1337c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
1338c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1339c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
1340c22aecc9S[email protected]         if (channel->handle != handle) continue;
134100d93d79Smatthias.ringwald         if (code & 1) {
1342b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1343b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
134400d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13454e32727eSmatthias.ringwald                 break;
134600d93d79Smatthias.ringwald             }
134700d93d79Smatthias.ringwald         } else {
1348b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
134900d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
135000d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13514e32727eSmatthias.ringwald                 break;
135200d93d79Smatthias.ringwald             }
135300d93d79Smatthias.ringwald         }
135400d93d79Smatthias.ringwald     }
135500d93d79Smatthias.ringwald }
135600d93d79Smatthias.ringwald 
135700d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
135800d93d79Smatthias.ringwald 
135900d93d79Smatthias.ringwald     // Get Channel ID
136000d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
136100d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
136200d93d79Smatthias.ringwald 
13635652b5ffS[email protected]     switch (channel_id) {
13645652b5ffS[email protected] 
13655652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13665652b5ffS[email protected] 
136700d93d79Smatthias.ringwald             uint16_t command_offset = 8;
136800d93d79Smatthias.ringwald             while (command_offset < size) {
136900d93d79Smatthias.ringwald 
137000d93d79Smatthias.ringwald                 // handle signaling commands
137100d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
137200d93d79Smatthias.ringwald 
137300d93d79Smatthias.ringwald                 // increment command_offset
137400d93d79Smatthias.ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
137500d93d79Smatthias.ringwald             }
13765652b5ffS[email protected]             break;
137700d93d79Smatthias.ringwald         }
137800d93d79Smatthias.ringwald 
13795652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
13805652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
13815652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13825652b5ffS[email protected]             }
13835652b5ffS[email protected]             break;
13845652b5ffS[email protected] 
13855652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
13865652b5ffS[email protected]             if (security_protocol_packet_handler) {
13875652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13885652b5ffS[email protected]             }
13895652b5ffS[email protected]             break;
13905652b5ffS[email protected] 
13911bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1392ccf076adS[email protected]             switch (packet[8]){
1393ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1394ccf076adS[email protected]                     uint16_t result = READ_BT_16(packet, 12);
1395ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1396ccf076adS[email protected]                     break;
1397ccf076adS[email protected]                 }
1398ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1399ccf076adS[email protected]                     uint8_t event[10];
1400ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1401ccf076adS[email protected]                     event[1] = 8;
1402ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1403da886c03S[email protected] 
1404da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1405da886c03S[email protected]                     if (connection){
1406da886c03S[email protected]                         int update_parameter = 1;
1407da886c03S[email protected]                         le_connection_parameter_range_t existing_range = gap_le_get_connection_parameter_range();
1408da886c03S[email protected]                         uint16_t le_conn_interval_min = READ_BT_16(packet,12);
1409da886c03S[email protected]                         uint16_t le_conn_interval_max = READ_BT_16(packet,14);
1410da886c03S[email protected]                         uint16_t le_conn_latency = READ_BT_16(packet,16);
1411da886c03S[email protected]                         uint16_t le_supervision_timeout = READ_BT_16(packet,18);
1412da886c03S[email protected] 
1413da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1414da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1415da886c03S[email protected] 
1416da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1417da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1418da886c03S[email protected] 
1419da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1420da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1421da886c03S[email protected] 
1422da886c03S[email protected]                         if (update_parameter){
1423da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1424da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1425da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1426da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1427da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1428da886c03S[email protected]                         } else {
1429da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1430da886c03S[email protected]                         }
1431da886c03S[email protected]                     }
1432da886c03S[email protected] 
1433ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1434ccf076adS[email protected]                     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event));
1435da886c03S[email protected] 
1436ccf076adS[email protected]                     break;
1437ccf076adS[email protected]                 }
1438ccf076adS[email protected]                 default: {
14391bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14401bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14411bbc0b23S[email protected]                     break;
14421bbc0b23S[email protected]                 }
1443ccf076adS[email protected]             }
1444ccf076adS[email protected]             break;
1445ccf076adS[email protected]         }
14461bbc0b23S[email protected] 
14475652b5ffS[email protected]         default: {
144800d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
144900d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
145000d93d79Smatthias.ringwald             if (channel) {
145158de5610Smatthias.ringwald                 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
145200d93d79Smatthias.ringwald             }
14535652b5ffS[email protected]             break;
14545652b5ffS[email protected]         }
14555652b5ffS[email protected]     }
145600d93d79Smatthias.ringwald }
145700d93d79Smatthias.ringwald 
14582718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
14592718e2e7Smatthias.ringwald     switch (packet_type) {
14602718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
14612718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
14622718e2e7Smatthias.ringwald             break;
14632718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
14642718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
14652718e2e7Smatthias.ringwald             break;
14662718e2e7Smatthias.ringwald         default:
14672718e2e7Smatthias.ringwald             break;
14682718e2e7Smatthias.ringwald     }
14691eb2563eS[email protected]     l2cap_run();
14702718e2e7Smatthias.ringwald }
147100d93d79Smatthias.ringwald 
147215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
147327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1474f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1475f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1476f62db1e3Smatthias.ringwald     // discard channel
14779dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1478f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1479d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1480c8e4258aSmatthias.ringwald }
14811e6aba47Smatthias.ringwald 
14829d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1483c22aecc9S[email protected]     linked_list_iterator_t it;
1484c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_services);
1485c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1486c22aecc9S[email protected]         l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it);
14879d9bbc01Smatthias.ringwald         if ( service->psm == psm){
14889d9bbc01Smatthias.ringwald             return service;
14899d9bbc01Smatthias.ringwald         };
14909d9bbc01Smatthias.ringwald     }
14919d9bbc01Smatthias.ringwald     return NULL;
14929d9bbc01Smatthias.ringwald }
14939d9bbc01Smatthias.ringwald 
149462f901dfS[email protected] void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){
1495e0abb8e7S[email protected] 
1496*e6f51008S[email protected]     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection);
1497e0abb8e7S[email protected] 
14984bb582b6Smatthias.ringwald     // check for alread registered psm
14994bb582b6Smatthias.ringwald     // TODO: emit error event
15009d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1501277abc2cSmatthias.ringwald     if (service) {
15029da54300S[email protected]         log_error("l2cap_register_service_internal: PSM %u already registered", psm);
150381476041Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
1504277abc2cSmatthias.ringwald         return;
1505277abc2cSmatthias.ringwald     }
15069d9bbc01Smatthias.ringwald 
15074bb582b6Smatthias.ringwald     // alloc structure
15084bb582b6Smatthias.ringwald     // TODO: emit error event
1509bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1510277abc2cSmatthias.ringwald     if (!service) {
15119da54300S[email protected]         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
15125842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
1513277abc2cSmatthias.ringwald         return;
1514277abc2cSmatthias.ringwald     }
15159d9bbc01Smatthias.ringwald 
15169d9bbc01Smatthias.ringwald     // fill in
15179d9bbc01Smatthias.ringwald     service->psm = psm;
15189d9bbc01Smatthias.ringwald     service->mtu = mtu;
15199d9bbc01Smatthias.ringwald     service->connection = connection;
1520d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
1521df3354fcS[email protected]     service->required_security_level = security_level;
15229d9bbc01Smatthias.ringwald 
15239d9bbc01Smatthias.ringwald     // add to services list
15249d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
1525c0e866bfSmatthias.ringwald 
1526c0e866bfSmatthias.ringwald     // enable page scan
1527c0e866bfSmatthias.ringwald     hci_connectable_control(1);
15285842b6d9Smatthias.ringwald 
15295842b6d9Smatthias.ringwald     // done
15305842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
15319d9bbc01Smatthias.ringwald }
15329d9bbc01Smatthias.ringwald 
153336944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
1534e0abb8e7S[email protected] 
1535e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1536e0abb8e7S[email protected] 
15379d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1538037d6e48Smatthias.ringwald     if (!service) return;
15399d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1540d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1541c0e866bfSmatthias.ringwald 
1542c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1543c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1544c0e866bfSmatthias.ringwald     hci_connectable_control(0);
15459d9bbc01Smatthias.ringwald }
15469d9bbc01Smatthias.ringwald 
15475652b5ffS[email protected] 
15485652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
15495652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
15505652b5ffS[email protected]     switch(channel_id){
15515652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
15525652b5ffS[email protected]             attribute_protocol_packet_handler = packet_handler;
15535652b5ffS[email protected]             break;
15545652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
15555652b5ffS[email protected]             security_protocol_packet_handler = packet_handler;
15565652b5ffS[email protected]             break;
15575652b5ffS[email protected]     }
15585652b5ffS[email protected] }
15595652b5ffS[email protected] 
1560ab2b01dcS[email protected] #ifdef HAVE_BLE
1561da886c03S[email protected] 
1562ab2b01dcS[email protected] // Request LE connection parameter update
1563ab2b01dcS[email protected] int l2cap_le_request_connection_parameter_update(uint16_t handle, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier){
1564a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
15659da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
1566ab2b01dcS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
1567ab2b01dcS[email protected]     }
15689da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
1569826f7347S[email protected]     hci_reserve_packet_buffer();
1570facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
1571ab2b01dcS[email protected]     uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier);
1572826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
1573ab2b01dcS[email protected] }
1574ab2b01dcS[email protected] #endif
1575ab2b01dcS[email protected] 
1576