xref: /btstack/src/l2cap.c (revision a35252c8fd2be7baa63ccf64f1ecfd7ff1090895)
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);
145c9dc710bS[email protected]     bt_store_16(event, 19, 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));
193ccf076adS[email protected]     (*packet_handler)(NULL, 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);
2238d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) 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;
247*a35252c8S[email protected]     return hci_can_send_acl_packet_now(channel->handle);
2487856fb31S[email protected] }
2497856fb31S[email protected] 
2503cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){
251*a35252c8S[email protected]     // TODO provide real handle
252*a35252c8S[email protected]     return hci_can_send_acl_packet_now(0x1234);
2533cab4fcaS[email protected] }
2543cab4fcaS[email protected] 
25596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
25696cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
25796cbd662Smatthias.ringwald     if (channel) {
25896cbd662Smatthias.ringwald         return channel->remote_mtu;
25996cbd662Smatthias.ringwald     }
26096cbd662Smatthias.ringwald     return 0;
26196cbd662Smatthias.ringwald }
26296cbd662Smatthias.ringwald 
2635932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){
264c22aecc9S[email protected]     linked_list_iterator_t it;
265c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
266c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
267c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
2685932bd7cS[email protected]         if ( &channel->rtx == ts) {
2695932bd7cS[email protected]             return channel;
2705932bd7cS[email protected]         }
2715932bd7cS[email protected]     }
2725932bd7cS[email protected]     return NULL;
2735932bd7cS[email protected] }
2745932bd7cS[email protected] 
2755932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){
2765932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2775932bd7cS[email protected]     if (!ts) return;
2785932bd7cS[email protected] 
2795932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2805932bd7cS[email protected] 
2815932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
2825932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
2835932bd7cS[email protected]     // notify client
2845932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
2855932bd7cS[email protected] 
2865932bd7cS[email protected]     // discard channel
2879dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
2885932bd7cS[email protected]     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
2895932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
2905932bd7cS[email protected] }
2915932bd7cS[email protected] 
2925932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
2935932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
2945932bd7cS[email protected]     run_loop_remove_timer(&channel->rtx);
2955932bd7cS[email protected] }
2965932bd7cS[email protected] 
2975932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
2985932bd7cS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
2995932bd7cS[email protected]     l2cap_stop_rtx(channel);
3005932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
3015932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
3025932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
3035932bd7cS[email protected] }
3045932bd7cS[email protected] 
3055932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3065932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3075932bd7cS[email protected]     l2cap_stop_rtx(channel);
3085932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
3095932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
3105932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
3115932bd7cS[email protected] }
3125932bd7cS[email protected] 
313ac301f95S[email protected] void l2cap_require_security_level_2_for_outgoing_sdp(){
314ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
315ac301f95S[email protected] }
316ac301f95S[email protected] 
317df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
318ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
319df3354fcS[email protected] }
3205932bd7cS[email protected] 
32158de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
322b1d43497Smatthias.ringwald 
323*a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
324b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
325b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
326b1d43497Smatthias.ringwald     }
327b1d43497Smatthias.ringwald 
3287b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
3292a373862S[email protected]     hci_reserve_packet_buffer();
330facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
33158de5610Smatthias.ringwald     va_list argptr;
33258de5610Smatthias.ringwald     va_start(argptr, identifier);
33370efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
33458de5610Smatthias.ringwald     va_end(argptr);
3357b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
336826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
33758de5610Smatthias.ringwald }
33858de5610Smatthias.ringwald 
33970efece1S[email protected] #ifdef HAVE_BLE
34070efece1S[email protected] int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
34170efece1S[email protected] 
342*a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
34370efece1S[email protected]         log_info("l2cap_send_signaling_packet, cannot send\n");
34470efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
34570efece1S[email protected]     }
34670efece1S[email protected] 
34770efece1S[email protected]     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
3482a373862S[email protected]     hci_reserve_packet_buffer();
349facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
35070efece1S[email protected]     va_list argptr;
35170efece1S[email protected]     va_start(argptr, identifier);
35270efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
35370efece1S[email protected]     va_end(argptr);
35470efece1S[email protected]     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
355826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
35670efece1S[email protected] }
35770efece1S[email protected] #endif
35870efece1S[email protected] 
359b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
360facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
361b1d43497Smatthias.ringwald }
3626218e6f1Smatthias.ringwald 
3636b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3646b4af23dS[email protected]     return hci_reserve_packet_buffer();
3656b4af23dS[email protected] }
3666b4af23dS[email protected] 
36768a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
36868a0fcf7S[email protected]     hci_release_packet_buffer();
36968a0fcf7S[email protected] }
37068a0fcf7S[email protected] 
37168a0fcf7S[email protected] 
372b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
373b1d43497Smatthias.ringwald 
374c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
375c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
376c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
377c8b9416aS[email protected]     }
378c8b9416aS[email protected] 
37958de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
380b1d43497Smatthias.ringwald     if (!channel) {
381c8b9416aS[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x\n", local_cid);
382b1d43497Smatthias.ringwald         return -1;   // TODO: define error
3836218e6f1Smatthias.ringwald     }
3846218e6f1Smatthias.ringwald 
385b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
386c8b9416aS[email protected]         log_error("l2cap_send_prepared cid 0x%02x, no credits!\n", local_cid);
387b1d43497Smatthias.ringwald         return -1;  // TODO: define error
388b1d43497Smatthias.ringwald     }
389b1d43497Smatthias.ringwald 
390*a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(channel->handle)){
391*a35252c8S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send\n", local_cid);
392*a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
393*a35252c8S[email protected]     }
394*a35252c8S[email protected] 
395b1d43497Smatthias.ringwald     --channel->packets_granted;
396b1d43497Smatthias.ringwald 
397c8b9416aS[email protected]     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used, credits left %u;\n",
398b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
399b1d43497Smatthias.ringwald 
400facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
401b1d43497Smatthias.ringwald 
402e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
403e9772277S[email protected] 
404e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
405e9772277S[email protected]     bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14));
40658de5610Smatthias.ringwald     // 2 - ACL length
40758de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
40858de5610Smatthias.ringwald     // 4 - L2CAP packet length
40958de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
41058de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
41158de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
41258de5610Smatthias.ringwald     // send
413826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
41491b99603Smatthias.ringwald 
41591b99603Smatthias.ringwald     l2cap_hand_out_credits();
41691b99603Smatthias.ringwald 
4176218e6f1Smatthias.ringwald     return err;
41858de5610Smatthias.ringwald }
41958de5610Smatthias.ringwald 
4202149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
4212149f12eSmatthias.ringwald 
422c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
423c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4242149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4252149f12eSmatthias.ringwald     }
4262149f12eSmatthias.ringwald 
427*a35252c8S[email protected]     if (!hci_can_send_prepared_acl_packet_now(handle)){
428c8b9416aS[email protected]         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send\n", handle, cid);
429c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
430c8b9416aS[email protected]     }
431c8b9416aS[email protected] 
432c8b9416aS[email protected]     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x\n", handle, cid);
4332149f12eSmatthias.ringwald 
434facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4352149f12eSmatthias.ringwald 
436e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
437e9772277S[email protected] 
438e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
439e9772277S[email protected]     bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
4402149f12eSmatthias.ringwald     // 2 - ACL length
4412149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
4422149f12eSmatthias.ringwald     // 4 - L2CAP packet length
4432149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
4442149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
4452149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 6, cid);
4462149f12eSmatthias.ringwald     // send
447826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4482149f12eSmatthias.ringwald 
4492149f12eSmatthias.ringwald     l2cap_hand_out_credits();
4502149f12eSmatthias.ringwald 
4512149f12eSmatthias.ringwald     return err;
4522149f12eSmatthias.ringwald }
4532149f12eSmatthias.ringwald 
454b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
455b1d43497Smatthias.ringwald 
456*a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
457*a35252c8S[email protected]     if (!channel) {
458*a35252c8S[email protected]         log_error("l2cap_send_internal no channel for cid 0x%02x\n", local_cid);
459*a35252c8S[email protected]         return -1;   // TODO: define error
460*a35252c8S[email protected]     }
461*a35252c8S[email protected] 
462*a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(channel->handle)){
463e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid);
464b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
465b1d43497Smatthias.ringwald     }
466b1d43497Smatthias.ringwald 
4672a373862S[email protected]     hci_reserve_packet_buffer();
468facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
469b1d43497Smatthias.ringwald 
470b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
471b1d43497Smatthias.ringwald 
472b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
473b1d43497Smatthias.ringwald }
474b1d43497Smatthias.ringwald 
4752149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
4762149f12eSmatthias.ringwald 
477*a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
478e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", cid);
4792149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4802149f12eSmatthias.ringwald     }
4812149f12eSmatthias.ringwald 
4822a373862S[email protected]     hci_reserve_packet_buffer();
483facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4842149f12eSmatthias.ringwald 
4852149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4862149f12eSmatthias.ringwald 
4872149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4882149f12eSmatthias.ringwald }
4892149f12eSmatthias.ringwald 
4900e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){
4910e37e417S[email protected]     return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data);
4920e37e417S[email protected] }
4930e37e417S[email protected] 
49428ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
49528ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
49628ca2b46S[email protected] }
49728ca2b46S[email protected] 
49828ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
49928ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
50028ca2b46S[email protected] }
50128ca2b46S[email protected] 
50228ca2b46S[email protected] 
503b1d43497Smatthias.ringwald 
5048158c421Smatthias.ringwald // MARK: L2CAP_RUN
5052cd0be45Smatthias.ringwald // process outstanding signaling tasks
5062cd0be45Smatthias.ringwald void l2cap_run(void){
5072b83fb7dSmatthias.ringwald 
5082b83fb7dSmatthias.ringwald     // check pending signaling responses
5092b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5102b83fb7dSmatthias.ringwald 
5112b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
512*a35252c8S[email protected] 
513*a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
514*a35252c8S[email protected] 
5152b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5162b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
51763a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
5182b83fb7dSmatthias.ringwald 
5192b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
5202b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5212b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5222bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5232bd8b7e7S[email protected]                 hci_disconnect_security_block(handle);
5242b360848Smatthias.ringwald                 break;
5252b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5262b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5272b83fb7dSmatthias.ringwald                 break;
5282b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5293b0484b3S[email protected]                 switch (infoType){
5303b0484b3S[email protected]                     case 1: { // Connectionless MTU
5313b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5323b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5333b0484b3S[email protected]                         break;
5343b0484b3S[email protected]                     }
5353b0484b3S[email protected]                     case 2: { // Extended Features Supported
5363b0484b3S[email protected]                         // extended features request supported, only supporing fixed channel map
5373b0484b3S[email protected]                         uint32_t features = 0x80;
5383b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5393b0484b3S[email protected]                         break;
5403b0484b3S[email protected]                     }
5413b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5423b0484b3S[email protected]                         uint8_t map[8];
5433b0484b3S[email protected]                         memset(map, 0, 8);
5443b0484b3S[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel
5453b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5463b0484b3S[email protected]                         break;
5473b0484b3S[email protected]                     }
5483b0484b3S[email protected]                     default:
5492b83fb7dSmatthias.ringwald                         // all other types are not supported
5502b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5513b0484b3S[email protected]                         break;
5522b83fb7dSmatthias.ringwald                 }
5532b83fb7dSmatthias.ringwald                 break;
55463a7246aSmatthias.ringwald             case COMMAND_REJECT:
5555ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
55670efece1S[email protected] #ifdef HAVE_BLE
55770efece1S[email protected]             case COMMAND_REJECT_LE:
55870efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
55963a7246aSmatthias.ringwald                 break;
56070efece1S[email protected] #endif
5612b83fb7dSmatthias.ringwald             default:
5622b83fb7dSmatthias.ringwald                 // should not happen
5632b83fb7dSmatthias.ringwald                 break;
5642b83fb7dSmatthias.ringwald         }
5652b83fb7dSmatthias.ringwald 
5662b83fb7dSmatthias.ringwald         // remove first item
5672b83fb7dSmatthias.ringwald         signaling_responses_pending--;
5682b83fb7dSmatthias.ringwald         int i;
5692b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
57076115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
5712b83fb7dSmatthias.ringwald         }
5722b83fb7dSmatthias.ringwald     }
5732b83fb7dSmatthias.ringwald 
574ae280e73Smatthias.ringwald     uint8_t  config_options[4];
575c22aecc9S[email protected]     linked_list_iterator_t it;
576c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
577c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
578c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
5792cd0be45Smatthias.ringwald 
580d94d3cafS[email protected]         if (!hci_can_send_command_packet_now()) break;
581*a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(channel->handle)) break;
5822cd0be45Smatthias.ringwald 
5837b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
5846e6710ebSmatthias.ringwald 
5852cd0be45Smatthias.ringwald         switch (channel->state){
5862cd0be45Smatthias.ringwald 
587df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
588ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
589a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
590ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
591a00031e2S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
592ad671560S[email protected]                 }
593ad671560S[email protected]                 break;
594ad671560S[email protected] 
59502b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
59664472d52Smatthias.ringwald                 // send connection request - set state first
59764472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
59802b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
5998f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
60002b22dc4Smatthias.ringwald                 break;
60102b22dc4Smatthias.ringwald 
602e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
6031eb2563eS[email protected]                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
604e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6059dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
606c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
607d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
608e7ff783cSmatthias.ringwald                 break;
609e7ff783cSmatthias.ringwald 
610552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
611fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
61228ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
6132a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
614552d92a1Smatthias.ringwald                 break;
615552d92a1Smatthias.ringwald 
6166fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
6176fdcc387Smatthias.ringwald                 // success, start l2cap handshake
618b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6196fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
6202a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6215932bd7cS[email protected]                 l2cap_start_rtx(channel);
6226fdcc387Smatthias.ringwald                 break;
6236fdcc387Smatthias.ringwald 
624fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
62573cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
62663a7246aSmatthias.ringwald                     uint16_t flags = 0;
62728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
62863a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
62963a7246aSmatthias.ringwald                         flags = 1;
63063a7246aSmatthias.ringwald                     } else {
63128ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
63263a7246aSmatthias.ringwald                     }
63363a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
63463a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
63563a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
63663a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
63763a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
63863a7246aSmatthias.ringwald                         bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
63963a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
64063a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
64163a7246aSmatthias.ringwald                     } else {
64263a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
64363a7246aSmatthias.ringwald                     }
64463a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
645fa8473a4Smatthias.ringwald                 }
64673cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
64728ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
64828ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
649b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
650ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
651ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
652ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
653b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6545932bd7cS[email protected]                     l2cap_start_rtx(channel);
655fa8473a4Smatthias.ringwald                 }
656fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
657552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
658552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
659552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
660fa8473a4Smatthias.ringwald                 }
661552d92a1Smatthias.ringwald                 break;
662552d92a1Smatthias.ringwald 
663e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
664b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6655932bd7cS[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 :)
666756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
667e7ff783cSmatthias.ringwald                 break;
668e7ff783cSmatthias.ringwald 
669e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
670b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6712cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
6722a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
6732cd0be45Smatthias.ringwald                 break;
6742cd0be45Smatthias.ringwald             default:
6752cd0be45Smatthias.ringwald                 break;
6762cd0be45Smatthias.ringwald         }
6772cd0be45Smatthias.ringwald     }
6782cd0be45Smatthias.ringwald }
6792cd0be45Smatthias.ringwald 
6804aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
681816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
682fa8c92f6Smatthias.ringwald }
683fa8c92f6Smatthias.ringwald 
6842df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){
6852df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
6865533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
6872df5dadcS[email protected]         // success, start l2cap handshake
6882df5dadcS[email protected]         channel->handle = handle;
6892df5dadcS[email protected]         channel->local_cid = l2cap_next_local_cid();
6902df5dadcS[email protected]         // check remote SSP feature first
6912df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
6922df5dadcS[email protected]     }
6932df5dadcS[email protected] }
6942df5dadcS[email protected] 
6952df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
6962df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
6972df5dadcS[email protected] 
6982df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
699ac301f95S[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));
7002df5dadcS[email protected]     if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7012df5dadcS[email protected]         // request security level 2
7022df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
7031429b2d6S[email protected]         gap_request_security_level(channel->handle, LEVEL_2);
7042df5dadcS[email protected]         return;
7052df5dadcS[email protected]     }
7062df5dadcS[email protected]     // fine, go ahead
7072df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7082df5dadcS[email protected] }
7092df5dadcS[email protected] 
7101e6aba47Smatthias.ringwald // open outgoing L2CAP channel
71115470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
71215470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
7131e6aba47Smatthias.ringwald 
714e0abb8e7S[email protected]     log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
715e0abb8e7S[email protected] 
7161e6aba47Smatthias.ringwald     // alloc structure
71728ca2b46S[email protected]     l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
7182b360848Smatthias.ringwald     if (!chan) {
7192b360848Smatthias.ringwald         // emit error event
7202b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
7212b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
7222b360848Smatthias.ringwald         dummy_channel.psm = psm;
7232b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
7242b360848Smatthias.ringwald         return;
7252b360848Smatthias.ringwald     }
7261d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
7272985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
7282985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
7299775e25bSmatthias.ringwald     }
7309775e25bSmatthias.ringwald 
7311e6aba47Smatthias.ringwald     // fill in
7321e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
7331e6aba47Smatthias.ringwald     chan->psm = psm;
7341e6aba47Smatthias.ringwald     chan->handle = 0;
7351e6aba47Smatthias.ringwald     chan->connection = connection;
7366b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
7372784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
73815470d27Smatthias.ringwald     chan->local_mtu = mtu;
7396218e6f1Smatthias.ringwald     chan->packets_granted = 0;
7406218e6f1Smatthias.ringwald 
7411e6aba47Smatthias.ringwald     // set initial state
74202b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
74373cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
744b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
745b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
7465533f01eS[email protected]     chan->required_security_level = LEVEL_0;
7471e6aba47Smatthias.ringwald 
7481e6aba47Smatthias.ringwald     // add to connections list
7491e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
7501e6aba47Smatthias.ringwald 
7512df5dadcS[email protected]     // check if hci connection is already usable
75296a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type((bd_addr_t*)address, BD_ADDR_TYPE_CLASSIC);
7532df5dadcS[email protected]     if (conn){
7545533f01eS[email protected]         log_info("l2cap_create_channel_internal, hci connection already exists");
7552df5dadcS[email protected]         l2cap_handle_connection_complete(conn->con_handle, chan);
7562df5dadcS[email protected]         // check ir remote supported fearures are already received
7572df5dadcS[email protected]         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
7582df5dadcS[email protected]             l2cap_handle_remote_supported_features_received(chan);
7592df5dadcS[email protected]         }
7602df5dadcS[email protected]     }
7612df5dadcS[email protected] 
76202b22dc4Smatthias.ringwald     l2cap_run();
76343625864Smatthias.ringwald }
76443625864Smatthias.ringwald 
765b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
766e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
767b35f641cSmatthias.ringwald     // find channel for local_cid
768b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
769f62db1e3Smatthias.ringwald     if (channel) {
770e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
771f62db1e3Smatthias.ringwald     }
7722cd0be45Smatthias.ringwald     // process
7732cd0be45Smatthias.ringwald     l2cap_run();
77443625864Smatthias.ringwald }
7751e6aba47Smatthias.ringwald 
776afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
777c22aecc9S[email protected]     linked_list_iterator_t it;
778c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
779c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
780c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
781c22aecc9S[email protected]         if ( BD_ADDR_CMP( channel->address, address) != 0) continue;
782c22aecc9S[email protected]         // channel for this address found
783c22aecc9S[email protected]         switch (channel->state){
784c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
785c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
786afde0c52Smatthias.ringwald                 // failure, forward error code
787afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
788afde0c52Smatthias.ringwald                 // discard channel
7899dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
790c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
791d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
792c22aecc9S[email protected]                 break;
793c22aecc9S[email protected]             default:
794c22aecc9S[email protected]                 break;
795afde0c52Smatthias.ringwald         }
796afde0c52Smatthias.ringwald     }
797afde0c52Smatthias.ringwald }
798afde0c52Smatthias.ringwald 
799afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
800c22aecc9S[email protected]     linked_list_iterator_t it;
801c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
802c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
803c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
804afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
8052df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
806afde0c52Smatthias.ringwald         }
807afde0c52Smatthias.ringwald     }
8086fdcc387Smatthias.ringwald     // process
8096fdcc387Smatthias.ringwald     l2cap_run();
810afde0c52Smatthias.ringwald }
811b448a0e7Smatthias.ringwald 
812afde0c52Smatthias.ringwald void l2cap_event_handler(uint8_t *packet, uint16_t size){
813afde0c52Smatthias.ringwald 
814afde0c52Smatthias.ringwald     bd_addr_t address;
815afde0c52Smatthias.ringwald     hci_con_handle_t handle;
816c22aecc9S[email protected]     linked_list_iterator_t it;
8172d00edd4Smatthias.ringwald     int hci_con_used;
818afde0c52Smatthias.ringwald 
819afde0c52Smatthias.ringwald     switch(packet[0]){
820afde0c52Smatthias.ringwald 
821afde0c52Smatthias.ringwald         // handle connection complete events
822afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
823afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
824afde0c52Smatthias.ringwald             if (packet[2] == 0){
825afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
826afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
827afde0c52Smatthias.ringwald             } else {
828afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
829afde0c52Smatthias.ringwald             }
830afde0c52Smatthias.ringwald             break;
831afde0c52Smatthias.ringwald 
832afde0c52Smatthias.ringwald         // handle successful create connection cancel command
833afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
834afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
835afde0c52Smatthias.ringwald                 if (packet[5] == 0){
836afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
837afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
838afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
83903cfbabcSmatthias.ringwald                 }
8401e6aba47Smatthias.ringwald             }
84139d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
84239d59809Smatthias.ringwald             break;
84339d59809Smatthias.ringwald 
84439d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
84539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
846afde0c52Smatthias.ringwald             break;
84727a923d0Smatthias.ringwald 
8481e6aba47Smatthias.ringwald         // handle disconnection complete events
849afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
850c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
851afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
852c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
853c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
854c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
855c22aecc9S[email protected]                 if (channel->handle != handle) continue;
85615ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
8579dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
858c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
859d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
86027a923d0Smatthias.ringwald             }
861afde0c52Smatthias.ringwald             break;
862fcadd0caSmatthias.ringwald 
8636218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
86402b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
8658d371091Smatthias.ringwald             l2cap_hand_out_credits();
8666218e6f1Smatthias.ringwald             break;
8676218e6f1Smatthias.ringwald 
868ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
869afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
87036944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
87180ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
8722d00edd4Smatthias.ringwald             hci_con_used = 0;
873c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
874c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
875c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
876c22aecc9S[email protected]                 if (channel->handle != handle) continue;
8772d00edd4Smatthias.ringwald                 hci_con_used = 1;
878c22aecc9S[email protected]                 break;
879ee091cf1Smatthias.ringwald             }
8802d00edd4Smatthias.ringwald             if (hci_con_used) break;
881d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
8829edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
883afde0c52Smatthias.ringwald             break;
884ee091cf1Smatthias.ringwald 
8856e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
886c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
887c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
888c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
889c22aecc9S[email protected]                 if (!channel->packet_handler) continue;
8906e6710ebSmatthias.ringwald                 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
8916e6710ebSmatthias.ringwald             }
8925652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
8935652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
8945652b5ffS[email protected]             }
8955652b5ffS[email protected]             if (security_protocol_packet_handler) {
896133efcfdSmatthias.ringwald                 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
8975652b5ffS[email protected]             }
8986e6710ebSmatthias.ringwald             break;
8996e6710ebSmatthias.ringwald 
900df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
901df3354fcS[email protected]             handle = READ_BT_16(packet, 3);
902c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
903c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
904c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
905df3354fcS[email protected]                 if (channel->handle != handle) continue;
9062df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
907df3354fcS[email protected]                 break;
908df3354fcS[email protected]             }
909c22aecc9S[email protected]             break;
910df3354fcS[email protected] 
911a00031e2S[email protected]         case GAP_SECURITY_LEVEL:
912a00031e2S[email protected]             handle = READ_BT_16(packet, 2);
913bd63148eS[email protected]             log_info("l2cap - security level update");
914c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
915c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
916c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
917f85a9399S[email protected]                 if (channel->handle != handle) continue;
9185533f01eS[email protected] 
919bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
920bd63148eS[email protected] 
9215533f01eS[email protected]                 gap_security_level_t actual_level = packet[4];
9225533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
9235533f01eS[email protected] 
924df3354fcS[email protected]                 switch (channel->state){
925df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
9265533f01eS[email protected]                         if (actual_level >= required_level){
927f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
928f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
9291eb2563eS[email protected]                         } else {
9301eb2563eS[email protected]                             channel->reason = 0x03; // security block
9311eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
9321eb2563eS[email protected]                         }
933df3354fcS[email protected]                         break;
934df3354fcS[email protected] 
935df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
9365533f01eS[email protected]                         if (actual_level >= required_level){
937df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
938df3354fcS[email protected]                         } else {
939df3354fcS[email protected]                             // disconnnect, authentication not good enough
940df3354fcS[email protected]                             hci_disconnect_security_block(handle);
941df3354fcS[email protected]                         }
942df3354fcS[email protected]                         break;
943df3354fcS[email protected] 
944df3354fcS[email protected]                     default:
945df3354fcS[email protected]                         break;
946df3354fcS[email protected]                 }
947f85a9399S[email protected]             }
948f85a9399S[email protected]             break;
949f85a9399S[email protected] 
950afde0c52Smatthias.ringwald         default:
951afde0c52Smatthias.ringwald             break;
952afde0c52Smatthias.ringwald     }
953afde0c52Smatthias.ringwald 
954e0707417S[email protected]     // pass on: main packet handler, att and sm packet handlers
955b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
956e0707417S[email protected]     if (attribute_protocol_packet_handler){
957e0707417S[email protected]         (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
958e0707417S[email protected]     }
959e0707417S[email protected]     if (security_protocol_packet_handler) {
960e0707417S[email protected]         (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
961e0707417S[email protected]     }
962bd63148eS[email protected] 
963bd63148eS[email protected]     l2cap_run();
9641e6aba47Smatthias.ringwald }
9651e6aba47Smatthias.ringwald 
966afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
967b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
968e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
969e7ff783cSmatthias.ringwald     l2cap_run();
97084836b65Smatthias.ringwald }
97184836b65Smatthias.ringwald 
9722b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
9734cf56b4aSmatthias.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."
9742b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
9752b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
9762b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
9772b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
9782b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
9792b360848Smatthias.ringwald         signaling_responses_pending++;
9802b360848Smatthias.ringwald         l2cap_run();
9812b360848Smatthias.ringwald     }
9822b360848Smatthias.ringwald }
9832b360848Smatthias.ringwald 
984b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
985645658c9Smatthias.ringwald 
986e0abb8e7S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x\n", handle, psm, source_cid);
987645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
988645658c9Smatthias.ringwald     if (!service) {
989645658c9Smatthias.ringwald         // 0x0002 PSM not supported
9902b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
991645658c9Smatthias.ringwald         return;
992645658c9Smatthias.ringwald     }
993645658c9Smatthias.ringwald 
9945061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
995645658c9Smatthias.ringwald     if (!hci_connection) {
9962b360848Smatthias.ringwald         //
9977d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
998645658c9Smatthias.ringwald         return;
999645658c9Smatthias.ringwald     }
10002bd8b7e7S[email protected] 
10012bd8b7e7S[email protected]     // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP
1002b087afb5S[email protected]     if (  hci_ssp_supported_on_both_sides(handle)
1003b087afb5S[email protected]     &&    gap_security_level(handle) == LEVEL_0
1004b087afb5S[email protected]     &&   !l2cap_security_level_0_allowed_for_PSM(psm)){
10052bd8b7e7S[email protected] 
10062bd8b7e7S[email protected]         // 0x0003 Security Block
10072bd8b7e7S[email protected]         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003);
10082bd8b7e7S[email protected]         return;
10092bd8b7e7S[email protected]     }
10102bd8b7e7S[email protected] 
10112bd8b7e7S[email protected] 
1012645658c9Smatthias.ringwald     // alloc structure
10137b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
101428ca2b46S[email protected]     l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
10152b360848Smatthias.ringwald     if (!channel){
10162b360848Smatthias.ringwald         // 0x0004 No resources available
10172b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10182b360848Smatthias.ringwald         return;
10192b360848Smatthias.ringwald     }
1020645658c9Smatthias.ringwald 
1021645658c9Smatthias.ringwald     // fill in
1022169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
1023169f8b28Smatthias.ringwald     channel->psm = psm;
1024169f8b28Smatthias.ringwald     channel->handle = handle;
1025169f8b28Smatthias.ringwald     channel->connection = service->connection;
1026f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1027b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1028b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1029fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
10300a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1031761b0451Smatthias.ringwald     channel->packets_granted = 0;
1032b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1033df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1034645658c9Smatthias.ringwald 
10351d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
10362985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
10372985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
10389775e25bSmatthias.ringwald     }
10399775e25bSmatthias.ringwald 
1040645658c9Smatthias.ringwald     // set initial state
1041df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1042ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1043e405ae81Smatthias.ringwald 
1044645658c9Smatthias.ringwald     // add to connections list
1045169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
1046645658c9Smatthias.ringwald 
1047f85a9399S[email protected]     // assert security requirements
10481eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1049e405ae81Smatthias.ringwald }
1050645658c9Smatthias.ringwald 
1051b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
1052e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1053b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1054e405ae81Smatthias.ringwald     if (!channel) {
10557d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
1056e405ae81Smatthias.ringwald         return;
1057e405ae81Smatthias.ringwald     }
1058e405ae81Smatthias.ringwald 
1059552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1060e405ae81Smatthias.ringwald 
1061552d92a1Smatthias.ringwald     // process
1062552d92a1Smatthias.ringwald     l2cap_run();
1063e405ae81Smatthias.ringwald }
1064645658c9Smatthias.ringwald 
1065b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
1066e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1067b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1068e405ae81Smatthias.ringwald     if (!channel) {
10697d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
1070e405ae81Smatthias.ringwald         return;
1071e405ae81Smatthias.ringwald     }
1072e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1073e7ff783cSmatthias.ringwald     channel->reason = reason;
1074e7ff783cSmatthias.ringwald     l2cap_run();
1075645658c9Smatthias.ringwald }
1076645658c9Smatthias.ringwald 
10772784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1078b1988dceSmatthias.ringwald 
1079b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1080b1988dceSmatthias.ringwald 
108163a7246aSmatthias.ringwald     uint16_t flags = READ_BT_16(command, 6);
108263a7246aSmatthias.ringwald     if (flags & 1) {
108363a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
108463a7246aSmatthias.ringwald     }
108563a7246aSmatthias.ringwald 
10862784b77dSmatthias.ringwald     // accept the other's configuration options
10873de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
10883de7c0caSmatthias.ringwald     uint16_t pos     = 8;
10893de7c0caSmatthias.ringwald     while (pos < end_pos){
109063a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
109163a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
109263a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
109363a7246aSmatthias.ringwald         pos++;
10941dc511deSmatthias.ringwald         uint8_t length = command[pos++];
10951dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
109663a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
10971dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
1098e0abb8e7S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
109963a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
110063a7246aSmatthias.ringwald         }
11010fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11020fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
11030fe7a9d0S[email protected]             channel->flush_timeout = READ_BT_16(command, pos);
11040fe7a9d0S[email protected]         }
110563a7246aSmatthias.ringwald         // check for unknown options
110663a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1107c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
110863a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11091dc511deSmatthias.ringwald         }
11101dc511deSmatthias.ringwald         pos += length;
11111dc511deSmatthias.ringwald     }
11122784b77dSmatthias.ringwald }
11132784b77dSmatthias.ringwald 
1114fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11157b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
111673cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
111773cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1118fa8473a4Smatthias.ringwald     return 1;
1119fa8473a4Smatthias.ringwald }
1120fa8473a4Smatthias.ringwald 
1121fa8473a4Smatthias.ringwald 
112200d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11231e6aba47Smatthias.ringwald 
112400d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
112500d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
112638e5900eSmatthias.ringwald     uint16_t result = 0;
11271e6aba47Smatthias.ringwald 
11285842b6d9Smatthias.ringwald     log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
1129b35f641cSmatthias.ringwald 
11309a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
11319a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
11329a011532Smatthias.ringwald         switch (channel->state){
1133fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
11349a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
11352b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
11369a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
11379a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
11389a011532Smatthias.ringwald                 break;
11399a011532Smatthias.ringwald 
11409a011532Smatthias.ringwald             default:
11419a011532Smatthias.ringwald                 // ignore in other states
11429a011532Smatthias.ringwald                 break;
11439a011532Smatthias.ringwald         }
11449a011532Smatthias.ringwald         return;
11459a011532Smatthias.ringwald     }
11469a011532Smatthias.ringwald 
114756081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
11481e6aba47Smatthias.ringwald     switch (channel->state) {
11491e6aba47Smatthias.ringwald 
11501e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
11511e6aba47Smatthias.ringwald             switch (code){
11521e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
11535932bd7cS[email protected]                     l2cap_stop_rtx(channel);
115400d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
115538e5900eSmatthias.ringwald                     switch (result) {
115638e5900eSmatthias.ringwald                         case 0:
1157169f8b28Smatthias.ringwald                             // successful connection
115800d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1159fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
116028ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
116138e5900eSmatthias.ringwald                             break;
116238e5900eSmatthias.ringwald                         case 1:
11635932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
11645932bd7cS[email protected]                             l2cap_start_ertx(channel);
116538e5900eSmatthias.ringwald                             break;
116638e5900eSmatthias.ringwald                         default:
1167eb920dbeSmatthias.ringwald                             // channel closed
1168eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1169f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
117038e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1171eb920dbeSmatthias.ringwald 
1172eb920dbeSmatthias.ringwald                             // drop link key if security block
1173eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
1174eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
1175eb920dbeSmatthias.ringwald                             }
1176eb920dbeSmatthias.ringwald 
1177eb920dbeSmatthias.ringwald                             // discard channel
1178eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1179d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
118038e5900eSmatthias.ringwald                             break;
11811e6aba47Smatthias.ringwald                     }
11821e6aba47Smatthias.ringwald                     break;
118338e5900eSmatthias.ringwald 
118438e5900eSmatthias.ringwald                 default:
11851e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
118638e5900eSmatthias.ringwald                     break;
11871e6aba47Smatthias.ringwald             }
11881e6aba47Smatthias.ringwald             break;
11891e6aba47Smatthias.ringwald 
1190fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1191fe9d8984S[email protected]             result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1192ae280e73Smatthias.ringwald             switch (code) {
1193ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
119428ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1195ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
119663a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
119763a7246aSmatthias.ringwald                         // only done if continuation not set
119863a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
119963a7246aSmatthias.ringwald                     }
1200ae280e73Smatthias.ringwald                     break;
12011e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12025932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12035932bd7cS[email protected]                     switch (result){
12045932bd7cS[email protected]                         case 0: // success
12055932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12065932bd7cS[email protected]                             break;
12075932bd7cS[email protected]                         case 4: // pending
12085932bd7cS[email protected]                             l2cap_start_ertx(channel);
12095932bd7cS[email protected]                             break;
12105932bd7cS[email protected]                         default:
1211fe9d8984S[email protected]                             // retry on negative result
1212fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1213fe9d8984S[email protected]                             break;
1214fe9d8984S[email protected]                     }
12155a67bd4aSmatthias.ringwald                     break;
12165a67bd4aSmatthias.ringwald                 default:
12175a67bd4aSmatthias.ringwald                     break;
12181e6aba47Smatthias.ringwald             }
1219fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1220fa8473a4Smatthias.ringwald                 // for open:
12215a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1222fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
12236218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
1224c8e4258aSmatthias.ringwald             }
1225c8e4258aSmatthias.ringwald             break;
1226f62db1e3Smatthias.ringwald 
1227f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1228f62db1e3Smatthias.ringwald             switch (code) {
1229f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
123027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
123127a923d0Smatthias.ringwald                     break;
12325a67bd4aSmatthias.ringwald                 default:
12335a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
12345a67bd4aSmatthias.ringwald                     break;
123527a923d0Smatthias.ringwald             }
123627a923d0Smatthias.ringwald             break;
123784836b65Smatthias.ringwald 
123884836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
123984836b65Smatthias.ringwald             // @TODO handle incoming requests
124084836b65Smatthias.ringwald             break;
124184836b65Smatthias.ringwald 
124284836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
124384836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
124484836b65Smatthias.ringwald             break;
124510642e45Smatthias.ringwald         default:
124610642e45Smatthias.ringwald             break;
124727a923d0Smatthias.ringwald     }
12487b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
124927a923d0Smatthias.ringwald }
125027a923d0Smatthias.ringwald 
125100d93d79Smatthias.ringwald 
125200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
125300d93d79Smatthias.ringwald 
125400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
125500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
125600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
125700d93d79Smatthias.ringwald 
125800d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
125900d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
126063a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
126100d93d79Smatthias.ringwald         return;
126200d93d79Smatthias.ringwald     }
126300d93d79Smatthias.ringwald 
126400d93d79Smatthias.ringwald     // general commands without an assigned channel
126500d93d79Smatthias.ringwald     switch(code) {
126600d93d79Smatthias.ringwald 
126700d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
126800d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
126900d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
127000d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
12712b83fb7dSmatthias.ringwald             return;
127200d93d79Smatthias.ringwald         }
127300d93d79Smatthias.ringwald 
12742b360848Smatthias.ringwald         case ECHO_REQUEST:
12752b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
12762b83fb7dSmatthias.ringwald             return;
127700d93d79Smatthias.ringwald 
127800d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
127900d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
12802b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
12812b83fb7dSmatthias.ringwald             return;
128200d93d79Smatthias.ringwald         }
128300d93d79Smatthias.ringwald 
128400d93d79Smatthias.ringwald         default:
128500d93d79Smatthias.ringwald             break;
128600d93d79Smatthias.ringwald     }
128700d93d79Smatthias.ringwald 
128800d93d79Smatthias.ringwald 
128900d93d79Smatthias.ringwald     // Get potential destination CID
129000d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
129100d93d79Smatthias.ringwald 
129200d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1293c22aecc9S[email protected]     linked_list_iterator_t it;
1294c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
1295c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1296c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
1297c22aecc9S[email protected]         if (channel->handle != handle) continue;
129800d93d79Smatthias.ringwald         if (code & 1) {
1299b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1300b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
130100d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13024e32727eSmatthias.ringwald                 break;
130300d93d79Smatthias.ringwald             }
130400d93d79Smatthias.ringwald         } else {
1305b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
130600d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
130700d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13084e32727eSmatthias.ringwald                 break;
130900d93d79Smatthias.ringwald             }
131000d93d79Smatthias.ringwald         }
131100d93d79Smatthias.ringwald     }
131200d93d79Smatthias.ringwald }
131300d93d79Smatthias.ringwald 
131400d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
131500d93d79Smatthias.ringwald 
131600d93d79Smatthias.ringwald     // Get Channel ID
131700d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
131800d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
131900d93d79Smatthias.ringwald 
13205652b5ffS[email protected]     switch (channel_id) {
13215652b5ffS[email protected] 
13225652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13235652b5ffS[email protected] 
132400d93d79Smatthias.ringwald             uint16_t command_offset = 8;
132500d93d79Smatthias.ringwald             while (command_offset < size) {
132600d93d79Smatthias.ringwald 
132700d93d79Smatthias.ringwald                 // handle signaling commands
132800d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
132900d93d79Smatthias.ringwald 
133000d93d79Smatthias.ringwald                 // increment command_offset
133100d93d79Smatthias.ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
133200d93d79Smatthias.ringwald             }
13335652b5ffS[email protected]             break;
133400d93d79Smatthias.ringwald         }
133500d93d79Smatthias.ringwald 
13365652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
13375652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
13385652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13395652b5ffS[email protected]             }
13405652b5ffS[email protected]             break;
13415652b5ffS[email protected] 
13425652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
13435652b5ffS[email protected]             if (security_protocol_packet_handler) {
13445652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13455652b5ffS[email protected]             }
13465652b5ffS[email protected]             break;
13475652b5ffS[email protected] 
13481bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1349ccf076adS[email protected]             switch (packet[8]){
1350ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1351ccf076adS[email protected]                     uint16_t result = READ_BT_16(packet, 12);
1352ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1353ccf076adS[email protected]                     break;
1354ccf076adS[email protected]                 }
1355ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1356ccf076adS[email protected]                     uint8_t event[10];
1357ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1358ccf076adS[email protected]                     event[1] = 8;
1359ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1360ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1361ccf076adS[email protected]                     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event));
1362ccf076adS[email protected]                     break;
1363ccf076adS[email protected]                 }
1364ccf076adS[email protected]                 default: {
13651bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
13661bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
13671bbc0b23S[email protected]                     break;
13681bbc0b23S[email protected]                 }
1369ccf076adS[email protected]             }
1370ccf076adS[email protected]             break;
1371ccf076adS[email protected]         }
13721bbc0b23S[email protected] 
13735652b5ffS[email protected]         default: {
137400d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
137500d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
137600d93d79Smatthias.ringwald             if (channel) {
137758de5610Smatthias.ringwald                 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
137800d93d79Smatthias.ringwald             }
13795652b5ffS[email protected]             break;
13805652b5ffS[email protected]         }
13815652b5ffS[email protected]     }
138200d93d79Smatthias.ringwald }
138300d93d79Smatthias.ringwald 
13842718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
13852718e2e7Smatthias.ringwald     switch (packet_type) {
13862718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
13872718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
13882718e2e7Smatthias.ringwald             break;
13892718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
13902718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
13912718e2e7Smatthias.ringwald             break;
13922718e2e7Smatthias.ringwald         default:
13932718e2e7Smatthias.ringwald             break;
13942718e2e7Smatthias.ringwald     }
13951eb2563eS[email protected]     l2cap_run();
13962718e2e7Smatthias.ringwald }
139700d93d79Smatthias.ringwald 
139815ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
139927a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1400f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1401f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1402f62db1e3Smatthias.ringwald     // discard channel
14039dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1404f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1405d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1406c8e4258aSmatthias.ringwald }
14071e6aba47Smatthias.ringwald 
14089d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1409c22aecc9S[email protected]     linked_list_iterator_t it;
1410c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_services);
1411c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1412c22aecc9S[email protected]         l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it);
14139d9bbc01Smatthias.ringwald         if ( service->psm == psm){
14149d9bbc01Smatthias.ringwald             return service;
14159d9bbc01Smatthias.ringwald         };
14169d9bbc01Smatthias.ringwald     }
14179d9bbc01Smatthias.ringwald     return NULL;
14189d9bbc01Smatthias.ringwald }
14199d9bbc01Smatthias.ringwald 
142062f901dfS[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){
1421e0abb8e7S[email protected] 
1422e0abb8e7S[email protected]     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1423e0abb8e7S[email protected] 
14244bb582b6Smatthias.ringwald     // check for alread registered psm
14254bb582b6Smatthias.ringwald     // TODO: emit error event
14269d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1427277abc2cSmatthias.ringwald     if (service) {
1428277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
142981476041Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
1430277abc2cSmatthias.ringwald         return;
1431277abc2cSmatthias.ringwald     }
14329d9bbc01Smatthias.ringwald 
14334bb582b6Smatthias.ringwald     // alloc structure
14344bb582b6Smatthias.ringwald     // TODO: emit error event
143528ca2b46S[email protected]     service = (l2cap_service_t *) btstack_memory_l2cap_service_get();
1436277abc2cSmatthias.ringwald     if (!service) {
1437277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
14385842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
1439277abc2cSmatthias.ringwald         return;
1440277abc2cSmatthias.ringwald     }
14419d9bbc01Smatthias.ringwald 
14429d9bbc01Smatthias.ringwald     // fill in
14439d9bbc01Smatthias.ringwald     service->psm = psm;
14449d9bbc01Smatthias.ringwald     service->mtu = mtu;
14459d9bbc01Smatthias.ringwald     service->connection = connection;
1446d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
1447df3354fcS[email protected]     service->required_security_level = security_level;
14489d9bbc01Smatthias.ringwald 
14499d9bbc01Smatthias.ringwald     // add to services list
14509d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
1451c0e866bfSmatthias.ringwald 
1452c0e866bfSmatthias.ringwald     // enable page scan
1453c0e866bfSmatthias.ringwald     hci_connectable_control(1);
14545842b6d9Smatthias.ringwald 
14555842b6d9Smatthias.ringwald     // done
14565842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
14579d9bbc01Smatthias.ringwald }
14589d9bbc01Smatthias.ringwald 
145936944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
1460e0abb8e7S[email protected] 
1461e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1462e0abb8e7S[email protected] 
14639d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1464037d6e48Smatthias.ringwald     if (!service) return;
14659d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1466d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1467c0e866bfSmatthias.ringwald 
1468c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1469c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1470c0e866bfSmatthias.ringwald     hci_connectable_control(0);
14719d9bbc01Smatthias.ringwald }
14729d9bbc01Smatthias.ringwald 
14739d9bbc01Smatthias.ringwald //
147436944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
14759d9bbc01Smatthias.ringwald 
1476c22aecc9S[email protected]     linked_list_iterator_t it;
1477c22aecc9S[email protected] 
1478c22aecc9S[email protected]     // close open channels
1479c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
1480c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1481c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
1482c22aecc9S[email protected]         if (channel->connection != connection) continue;
1483cb8eb7e6Smatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
14846128d740S[email protected]         channel->connection = NULL;
1485c52bf64dSmatthias.ringwald     }
14869d9bbc01Smatthias.ringwald 
1487645658c9Smatthias.ringwald     // unregister services
1488c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_services);
1489c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1490c22aecc9S[email protected]         l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it);
1491c22aecc9S[email protected]         if (service->connection != connection) continue;
1492c22aecc9S[email protected]         linked_list_iterator_remove(&it);
1493d3a9df87Smatthias.ringwald         btstack_memory_l2cap_service_free(service);
1494645658c9Smatthias.ringwald     }
1495cb8eb7e6Smatthias.ringwald 
1496cb8eb7e6Smatthias.ringwald     // process
1497cb8eb7e6Smatthias.ringwald     l2cap_run();
1498c52bf64dSmatthias.ringwald }
14995652b5ffS[email protected] 
15005652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
15015652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
15025652b5ffS[email protected]     switch(channel_id){
15035652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
15045652b5ffS[email protected]             attribute_protocol_packet_handler = packet_handler;
15055652b5ffS[email protected]             break;
15065652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
15075652b5ffS[email protected]             security_protocol_packet_handler = packet_handler;
15085652b5ffS[email protected]             break;
15095652b5ffS[email protected]     }
15105652b5ffS[email protected] }
15115652b5ffS[email protected] 
1512ab2b01dcS[email protected] #ifdef HAVE_BLE
1513ab2b01dcS[email protected] // Request LE connection parameter update
1514ab2b01dcS[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){
1515*a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
1516ab2b01dcS[email protected]         log_info("l2cap_send_signaling_packet, cannot send\n");
1517ab2b01dcS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
1518ab2b01dcS[email protected]     }
1519ab2b01dcS[email protected]     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
1520826f7347S[email protected]     hci_reserve_packet_buffer();
1521facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
1522ab2b01dcS[email protected]     uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier);
1523826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
1524ab2b01dcS[email protected] }
1525ab2b01dcS[email protected] #endif
1526ab2b01dcS[email protected] 
1527