xref: /btstack/src/l2cap.c (revision c22aecc9a7a5a9fba9f658b283cd7c2ff7be6ec7)
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 
1755842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
176e0abb8e7S[email protected]     log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm);
1775842b6d9Smatthias.ringwald     uint8_t event[5];
1785842b6d9Smatthias.ringwald     event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
1795842b6d9Smatthias.ringwald     event[1] = sizeof(event) - 2;
1805842b6d9Smatthias.ringwald     event[2] = status;
1815842b6d9Smatthias.ringwald     bt_store_16(event, 3, psm);
1825842b6d9Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1835842b6d9Smatthias.ringwald     (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
1845842b6d9Smatthias.ringwald }
1855842b6d9Smatthias.ringwald 
1866218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
187e0abb8e7S[email protected] 
188e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits);
1896218e6f1Smatthias.ringwald     // track credits
1906218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1916218e6f1Smatthias.ringwald 
1926218e6f1Smatthias.ringwald     uint8_t event[5];
1936218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1946218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1956218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1966218e6f1Smatthias.ringwald     event[4] = credits;
1976218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1986218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1996218e6f1Smatthias.ringwald }
2006218e6f1Smatthias.ringwald 
201808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
202808a48abSmatthias.ringwald     new_credits_blocked = blocked;
203808a48abSmatthias.ringwald }
204808a48abSmatthias.ringwald 
20540d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
206808a48abSmatthias.ringwald 
207808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
208808a48abSmatthias.ringwald 
209*c22aecc9S[email protected]     linked_list_iterator_t it;
210*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
211*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
212*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
2138d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
2140e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2154c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2168d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2178d371091Smatthias.ringwald         }
2188d371091Smatthias.ringwald     }
2198d371091Smatthias.ringwald }
2208d371091Smatthias.ringwald 
221b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
222*c22aecc9S[email protected]     linked_list_iterator_t it;
223*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
224*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
225*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
226b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
227f62db1e3Smatthias.ringwald             return channel;
228f62db1e3Smatthias.ringwald         }
229f62db1e3Smatthias.ringwald     }
230f62db1e3Smatthias.ringwald     return NULL;
231f62db1e3Smatthias.ringwald }
232f62db1e3Smatthias.ringwald 
2336b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2346b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2356b1fde37Smatthias.ringwald     if (!channel) return 0;
2366b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2377856fb31S[email protected]     return hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET);
2387856fb31S[email protected] }
2397856fb31S[email protected] 
2403cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){
2413cab4fcaS[email protected]     return hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET);
2423cab4fcaS[email protected] }
2433cab4fcaS[email protected] 
24496cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
24596cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
24696cbd662Smatthias.ringwald     if (channel) {
24796cbd662Smatthias.ringwald         return channel->remote_mtu;
24896cbd662Smatthias.ringwald     }
24996cbd662Smatthias.ringwald     return 0;
25096cbd662Smatthias.ringwald }
25196cbd662Smatthias.ringwald 
2525932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){
253*c22aecc9S[email protected]     linked_list_iterator_t it;
254*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
255*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
256*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
2575932bd7cS[email protected]         if ( &channel->rtx == ts) {
2585932bd7cS[email protected]             return channel;
2595932bd7cS[email protected]         }
2605932bd7cS[email protected]     }
2615932bd7cS[email protected]     return NULL;
2625932bd7cS[email protected] }
2635932bd7cS[email protected] 
2645932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){
2655932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2665932bd7cS[email protected]     if (!ts) return;
2675932bd7cS[email protected] 
2685932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2695932bd7cS[email protected] 
2705932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
2715932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
2725932bd7cS[email protected]     // notify client
2735932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
2745932bd7cS[email protected] 
2755932bd7cS[email protected]     // discard channel
2769dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
2775932bd7cS[email protected]     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
2785932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
2795932bd7cS[email protected] }
2805932bd7cS[email protected] 
2815932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
2825932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
2835932bd7cS[email protected]     run_loop_remove_timer(&channel->rtx);
2845932bd7cS[email protected] }
2855932bd7cS[email protected] 
2865932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
2875932bd7cS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
2885932bd7cS[email protected]     l2cap_stop_rtx(channel);
2895932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
2905932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
2915932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
2925932bd7cS[email protected] }
2935932bd7cS[email protected] 
2945932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
2955932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
2965932bd7cS[email protected]     l2cap_stop_rtx(channel);
2975932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
2985932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
2995932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
3005932bd7cS[email protected] }
3015932bd7cS[email protected] 
302ac301f95S[email protected] void l2cap_require_security_level_2_for_outgoing_sdp(){
303ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
304ac301f95S[email protected] }
305ac301f95S[email protected] 
306df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
307ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
308df3354fcS[email protected] }
3095932bd7cS[email protected] 
31058de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
311b1d43497Smatthias.ringwald 
3122a373862S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){
313b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
314b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
315b1d43497Smatthias.ringwald     }
316b1d43497Smatthias.ringwald 
3177b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
3182a373862S[email protected]     hci_reserve_packet_buffer();
319facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
32058de5610Smatthias.ringwald     va_list argptr;
32158de5610Smatthias.ringwald     va_start(argptr, identifier);
32270efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
32358de5610Smatthias.ringwald     va_end(argptr);
3247b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
325826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
32658de5610Smatthias.ringwald }
32758de5610Smatthias.ringwald 
32870efece1S[email protected] #ifdef HAVE_BLE
32970efece1S[email protected] int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
33070efece1S[email protected] 
3312a373862S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){
33270efece1S[email protected]         log_info("l2cap_send_signaling_packet, cannot send\n");
33370efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
33470efece1S[email protected]     }
33570efece1S[email protected] 
33670efece1S[email protected]     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
3372a373862S[email protected]     hci_reserve_packet_buffer();
338facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
33970efece1S[email protected]     va_list argptr;
34070efece1S[email protected]     va_start(argptr, identifier);
34170efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
34270efece1S[email protected]     va_end(argptr);
34370efece1S[email protected]     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
344826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
34570efece1S[email protected] }
34670efece1S[email protected] #endif
34770efece1S[email protected] 
348b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
349facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
350b1d43497Smatthias.ringwald }
3516218e6f1Smatthias.ringwald 
3526b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3536b4af23dS[email protected]     return hci_reserve_packet_buffer();
3546b4af23dS[email protected] }
3556b4af23dS[email protected] 
35668a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
35768a0fcf7S[email protected]     hci_release_packet_buffer();
35868a0fcf7S[email protected] }
35968a0fcf7S[email protected] 
36068a0fcf7S[email protected] 
361b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
362b1d43497Smatthias.ringwald 
363c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
364c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
365c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
366c8b9416aS[email protected]     }
367c8b9416aS[email protected] 
368b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
369c8b9416aS[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send\n", local_cid);
370808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3718ea03fa5Smatthias.ringwald     }
3726218e6f1Smatthias.ringwald 
37358de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
374b1d43497Smatthias.ringwald     if (!channel) {
375c8b9416aS[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x\n", local_cid);
376b1d43497Smatthias.ringwald         return -1;   // TODO: define error
3776218e6f1Smatthias.ringwald     }
3786218e6f1Smatthias.ringwald 
379b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
380c8b9416aS[email protected]         log_error("l2cap_send_prepared cid 0x%02x, no credits!\n", local_cid);
381b1d43497Smatthias.ringwald         return -1;  // TODO: define error
382b1d43497Smatthias.ringwald     }
383b1d43497Smatthias.ringwald 
384b1d43497Smatthias.ringwald     --channel->packets_granted;
385b1d43497Smatthias.ringwald 
386c8b9416aS[email protected]     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used, credits left %u;\n",
387b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
388b1d43497Smatthias.ringwald 
389facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
390b1d43497Smatthias.ringwald 
391e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
392e9772277S[email protected] 
393e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
394e9772277S[email protected]     bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14));
39558de5610Smatthias.ringwald     // 2 - ACL length
39658de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
39758de5610Smatthias.ringwald     // 4 - L2CAP packet length
39858de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
39958de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
40058de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
40158de5610Smatthias.ringwald     // send
402826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
40391b99603Smatthias.ringwald 
40491b99603Smatthias.ringwald     l2cap_hand_out_credits();
40591b99603Smatthias.ringwald 
4066218e6f1Smatthias.ringwald     return err;
40758de5610Smatthias.ringwald }
40858de5610Smatthias.ringwald 
4092149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
4102149f12eSmatthias.ringwald 
411c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
412c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4132149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4142149f12eSmatthias.ringwald     }
4152149f12eSmatthias.ringwald 
416c8b9416aS[email protected]     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
417c8b9416aS[email protected]         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send\n", handle, cid);
418c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
419c8b9416aS[email protected]     }
420c8b9416aS[email protected] 
421c8b9416aS[email protected]     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x\n", handle, cid);
4222149f12eSmatthias.ringwald 
423facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4242149f12eSmatthias.ringwald 
425e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
426e9772277S[email protected] 
427e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
428e9772277S[email protected]     bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
4292149f12eSmatthias.ringwald     // 2 - ACL length
4302149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
4312149f12eSmatthias.ringwald     // 4 - L2CAP packet length
4322149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
4332149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
4342149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 6, cid);
4352149f12eSmatthias.ringwald     // send
436826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4372149f12eSmatthias.ringwald 
4382149f12eSmatthias.ringwald     l2cap_hand_out_credits();
4392149f12eSmatthias.ringwald 
4402149f12eSmatthias.ringwald     return err;
4412149f12eSmatthias.ringwald }
4422149f12eSmatthias.ringwald 
443b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
444b1d43497Smatthias.ringwald 
4452a373862S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){
446e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid);
447b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
448b1d43497Smatthias.ringwald     }
449b1d43497Smatthias.ringwald 
4502a373862S[email protected]     hci_reserve_packet_buffer();
451facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
452b1d43497Smatthias.ringwald 
453b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
454b1d43497Smatthias.ringwald 
455b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
456b1d43497Smatthias.ringwald }
457b1d43497Smatthias.ringwald 
4582149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
4592149f12eSmatthias.ringwald 
4602a373862S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){
461e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", cid);
4622149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4632149f12eSmatthias.ringwald     }
4642149f12eSmatthias.ringwald 
4652a373862S[email protected]     hci_reserve_packet_buffer();
466facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4672149f12eSmatthias.ringwald 
4682149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4692149f12eSmatthias.ringwald 
4702149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4712149f12eSmatthias.ringwald }
4722149f12eSmatthias.ringwald 
4730e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){
4740e37e417S[email protected]     return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data);
4750e37e417S[email protected] }
4760e37e417S[email protected] 
47728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
47828ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
47928ca2b46S[email protected] }
48028ca2b46S[email protected] 
48128ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
48228ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
48328ca2b46S[email protected] }
48428ca2b46S[email protected] 
48528ca2b46S[email protected] 
486b1d43497Smatthias.ringwald 
4878158c421Smatthias.ringwald // MARK: L2CAP_RUN
4882cd0be45Smatthias.ringwald // process outstanding signaling tasks
4892cd0be45Smatthias.ringwald void l2cap_run(void){
4902b83fb7dSmatthias.ringwald 
4912b83fb7dSmatthias.ringwald     // check pending signaling responses
4922b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
4932b83fb7dSmatthias.ringwald 
4942a373862S[email protected]         if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)) break;
4952b83fb7dSmatthias.ringwald 
4962b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
4972b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
4982b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
49963a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
5002b83fb7dSmatthias.ringwald 
5012b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
5022b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5032b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5042bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5052bd8b7e7S[email protected]                 hci_disconnect_security_block(handle);
5062b360848Smatthias.ringwald                 break;
5072b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5082b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5092b83fb7dSmatthias.ringwald                 break;
5102b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5113b0484b3S[email protected]                 switch (infoType){
5123b0484b3S[email protected]                     case 1: { // Connectionless MTU
5133b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5143b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5153b0484b3S[email protected]                         break;
5163b0484b3S[email protected]                     }
5173b0484b3S[email protected]                     case 2: { // Extended Features Supported
5183b0484b3S[email protected]                         // extended features request supported, only supporing fixed channel map
5193b0484b3S[email protected]                         uint32_t features = 0x80;
5203b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5213b0484b3S[email protected]                         break;
5223b0484b3S[email protected]                     }
5233b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5243b0484b3S[email protected]                         uint8_t map[8];
5253b0484b3S[email protected]                         memset(map, 0, 8);
5263b0484b3S[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel
5273b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5283b0484b3S[email protected]                         break;
5293b0484b3S[email protected]                     }
5303b0484b3S[email protected]                     default:
5312b83fb7dSmatthias.ringwald                         // all other types are not supported
5322b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5333b0484b3S[email protected]                         break;
5342b83fb7dSmatthias.ringwald                 }
5352b83fb7dSmatthias.ringwald                 break;
53663a7246aSmatthias.ringwald             case COMMAND_REJECT:
5375ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
53870efece1S[email protected] #ifdef HAVE_BLE
53970efece1S[email protected]             case COMMAND_REJECT_LE:
54070efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
54163a7246aSmatthias.ringwald                 break;
54270efece1S[email protected] #endif
5432b83fb7dSmatthias.ringwald             default:
5442b83fb7dSmatthias.ringwald                 // should not happen
5452b83fb7dSmatthias.ringwald                 break;
5462b83fb7dSmatthias.ringwald         }
5472b83fb7dSmatthias.ringwald 
5482b83fb7dSmatthias.ringwald         // remove first item
5492b83fb7dSmatthias.ringwald         signaling_responses_pending--;
5502b83fb7dSmatthias.ringwald         int i;
5512b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
55276115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
5532b83fb7dSmatthias.ringwald         }
5542b83fb7dSmatthias.ringwald     }
5552b83fb7dSmatthias.ringwald 
556ae280e73Smatthias.ringwald     uint8_t  config_options[4];
557*c22aecc9S[email protected]     linked_list_iterator_t it;
558*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
559*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
560*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
5612cd0be45Smatthias.ringwald 
5622a373862S[email protected]         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) break;
5632a373862S[email protected]         if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)) break;
5642cd0be45Smatthias.ringwald 
5657b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
5666e6710ebSmatthias.ringwald 
5672cd0be45Smatthias.ringwald         switch (channel->state){
5682cd0be45Smatthias.ringwald 
569df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
570ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
571a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
572ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
573a00031e2S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
574ad671560S[email protected]                 }
575ad671560S[email protected]                 break;
576ad671560S[email protected] 
57702b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
57864472d52Smatthias.ringwald                 // send connection request - set state first
57964472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
58002b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
5818f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
58202b22dc4Smatthias.ringwald                 break;
58302b22dc4Smatthias.ringwald 
584e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
5851eb2563eS[email protected]                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
586e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
5879dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
588*c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
589d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
590e7ff783cSmatthias.ringwald                 break;
591e7ff783cSmatthias.ringwald 
592552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
593fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
59428ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
5952a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
596552d92a1Smatthias.ringwald                 break;
597552d92a1Smatthias.ringwald 
5986fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
5996fdcc387Smatthias.ringwald                 // success, start l2cap handshake
600b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6016fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
6022a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6035932bd7cS[email protected]                 l2cap_start_rtx(channel);
6046fdcc387Smatthias.ringwald                 break;
6056fdcc387Smatthias.ringwald 
606fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
60773cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
60863a7246aSmatthias.ringwald                     uint16_t flags = 0;
60928ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
61063a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
61163a7246aSmatthias.ringwald                         flags = 1;
61263a7246aSmatthias.ringwald                     } else {
61328ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
61463a7246aSmatthias.ringwald                     }
61563a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
61663a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
61763a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
61863a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
61963a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
62063a7246aSmatthias.ringwald                         bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
62163a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
62263a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
62363a7246aSmatthias.ringwald                     } else {
62463a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
62563a7246aSmatthias.ringwald                     }
62663a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
627fa8473a4Smatthias.ringwald                 }
62873cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
62928ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
63028ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
631b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
632ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
633ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
634ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
635b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6365932bd7cS[email protected]                     l2cap_start_rtx(channel);
637fa8473a4Smatthias.ringwald                 }
638fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
639552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
640552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
641552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
642fa8473a4Smatthias.ringwald                 }
643552d92a1Smatthias.ringwald                 break;
644552d92a1Smatthias.ringwald 
645e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
646b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6475932bd7cS[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 :)
648756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
649e7ff783cSmatthias.ringwald                 break;
650e7ff783cSmatthias.ringwald 
651e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
652b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6532cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
6542a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
6552cd0be45Smatthias.ringwald                 break;
6562cd0be45Smatthias.ringwald             default:
6572cd0be45Smatthias.ringwald                 break;
6582cd0be45Smatthias.ringwald         }
6592cd0be45Smatthias.ringwald     }
6602cd0be45Smatthias.ringwald }
6612cd0be45Smatthias.ringwald 
6624aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
663816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
664fa8c92f6Smatthias.ringwald }
665fa8c92f6Smatthias.ringwald 
6662df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){
6672df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
6685533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
6692df5dadcS[email protected]         // success, start l2cap handshake
6702df5dadcS[email protected]         channel->handle = handle;
6712df5dadcS[email protected]         channel->local_cid = l2cap_next_local_cid();
6722df5dadcS[email protected]         // check remote SSP feature first
6732df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
6742df5dadcS[email protected]     }
6752df5dadcS[email protected] }
6762df5dadcS[email protected] 
6772df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
6782df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
6792df5dadcS[email protected] 
6802df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
681ac301f95S[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));
6822df5dadcS[email protected]     if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
6832df5dadcS[email protected]         // request security level 2
6842df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
6851429b2d6S[email protected]         gap_request_security_level(channel->handle, LEVEL_2);
6862df5dadcS[email protected]         return;
6872df5dadcS[email protected]     }
6882df5dadcS[email protected]     // fine, go ahead
6892df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
6902df5dadcS[email protected] }
6912df5dadcS[email protected] 
6921e6aba47Smatthias.ringwald // open outgoing L2CAP channel
69315470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
69415470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
6951e6aba47Smatthias.ringwald 
696e0abb8e7S[email protected]     log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
697e0abb8e7S[email protected] 
6981e6aba47Smatthias.ringwald     // alloc structure
69928ca2b46S[email protected]     l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
7002b360848Smatthias.ringwald     if (!chan) {
7012b360848Smatthias.ringwald         // emit error event
7022b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
7032b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
7042b360848Smatthias.ringwald         dummy_channel.psm = psm;
7052b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
7062b360848Smatthias.ringwald         return;
7072b360848Smatthias.ringwald     }
7081d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
7092985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
7102985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
7119775e25bSmatthias.ringwald     }
7129775e25bSmatthias.ringwald 
7131e6aba47Smatthias.ringwald     // fill in
7141e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
7151e6aba47Smatthias.ringwald     chan->psm = psm;
7161e6aba47Smatthias.ringwald     chan->handle = 0;
7171e6aba47Smatthias.ringwald     chan->connection = connection;
7186b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
7192784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
72015470d27Smatthias.ringwald     chan->local_mtu = mtu;
7216218e6f1Smatthias.ringwald     chan->packets_granted = 0;
7226218e6f1Smatthias.ringwald 
7231e6aba47Smatthias.ringwald     // set initial state
72402b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
72573cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
726b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
727b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
7285533f01eS[email protected]     chan->required_security_level = LEVEL_0;
7291e6aba47Smatthias.ringwald 
7301e6aba47Smatthias.ringwald     // add to connections list
7311e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
7321e6aba47Smatthias.ringwald 
7332df5dadcS[email protected]     // check if hci connection is already usable
73496a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type((bd_addr_t*)address, BD_ADDR_TYPE_CLASSIC);
7352df5dadcS[email protected]     if (conn){
7365533f01eS[email protected]         log_info("l2cap_create_channel_internal, hci connection already exists");
7372df5dadcS[email protected]         l2cap_handle_connection_complete(conn->con_handle, chan);
7382df5dadcS[email protected]         // check ir remote supported fearures are already received
7392df5dadcS[email protected]         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
7402df5dadcS[email protected]             l2cap_handle_remote_supported_features_received(chan);
7412df5dadcS[email protected]         }
7422df5dadcS[email protected]     }
7432df5dadcS[email protected] 
74402b22dc4Smatthias.ringwald     l2cap_run();
74543625864Smatthias.ringwald }
74643625864Smatthias.ringwald 
747b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
748e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
749b35f641cSmatthias.ringwald     // find channel for local_cid
750b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
751f62db1e3Smatthias.ringwald     if (channel) {
752e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
753f62db1e3Smatthias.ringwald     }
7542cd0be45Smatthias.ringwald     // process
7552cd0be45Smatthias.ringwald     l2cap_run();
75643625864Smatthias.ringwald }
7571e6aba47Smatthias.ringwald 
758afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
759*c22aecc9S[email protected]     linked_list_iterator_t it;
760*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
761*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
762*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
763*c22aecc9S[email protected]         if ( BD_ADDR_CMP( channel->address, address) != 0) continue;
764*c22aecc9S[email protected]         // channel for this address found
765*c22aecc9S[email protected]         switch (channel->state){
766*c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
767*c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
768afde0c52Smatthias.ringwald                 // failure, forward error code
769afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
770afde0c52Smatthias.ringwald                 // discard channel
7719dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
772*c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
773d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
774*c22aecc9S[email protected]                 break;
775*c22aecc9S[email protected]             default:
776*c22aecc9S[email protected]                 break;
777afde0c52Smatthias.ringwald         }
778afde0c52Smatthias.ringwald     }
779afde0c52Smatthias.ringwald }
780afde0c52Smatthias.ringwald 
781afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
782*c22aecc9S[email protected]     linked_list_iterator_t it;
783*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
784*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
785*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
786afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
7872df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
788afde0c52Smatthias.ringwald         }
789afde0c52Smatthias.ringwald     }
7906fdcc387Smatthias.ringwald     // process
7916fdcc387Smatthias.ringwald     l2cap_run();
792afde0c52Smatthias.ringwald }
793b448a0e7Smatthias.ringwald 
794afde0c52Smatthias.ringwald void l2cap_event_handler(uint8_t *packet, uint16_t size){
795afde0c52Smatthias.ringwald 
796afde0c52Smatthias.ringwald     bd_addr_t address;
797afde0c52Smatthias.ringwald     hci_con_handle_t handle;
798*c22aecc9S[email protected]     linked_list_iterator_t it;
7992d00edd4Smatthias.ringwald     int hci_con_used;
800afde0c52Smatthias.ringwald 
801afde0c52Smatthias.ringwald     switch(packet[0]){
802afde0c52Smatthias.ringwald 
803afde0c52Smatthias.ringwald         // handle connection complete events
804afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
805afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
806afde0c52Smatthias.ringwald             if (packet[2] == 0){
807afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
808afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
809afde0c52Smatthias.ringwald             } else {
810afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
811afde0c52Smatthias.ringwald             }
812afde0c52Smatthias.ringwald             break;
813afde0c52Smatthias.ringwald 
814afde0c52Smatthias.ringwald         // handle successful create connection cancel command
815afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
816afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
817afde0c52Smatthias.ringwald                 if (packet[5] == 0){
818afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
819afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
820afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
82103cfbabcSmatthias.ringwald                 }
8221e6aba47Smatthias.ringwald             }
82339d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
82439d59809Smatthias.ringwald             break;
82539d59809Smatthias.ringwald 
82639d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
82739d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
828afde0c52Smatthias.ringwald             break;
82927a923d0Smatthias.ringwald 
8301e6aba47Smatthias.ringwald         // handle disconnection complete events
831afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
832*c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
833afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
834*c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
835*c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
836*c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
837*c22aecc9S[email protected]                 if (channel->handle != handle) continue;
83815ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
8399dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
840*c22aecc9S[email protected]                 linked_list_iterator_remove(&it);
841d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
84227a923d0Smatthias.ringwald             }
843afde0c52Smatthias.ringwald             break;
844fcadd0caSmatthias.ringwald 
8456218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
84602b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
8478d371091Smatthias.ringwald             l2cap_hand_out_credits();
8486218e6f1Smatthias.ringwald             break;
8496218e6f1Smatthias.ringwald 
850ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
851afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
85236944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
85380ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
8542d00edd4Smatthias.ringwald             hci_con_used = 0;
855*c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
856*c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
857*c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
858*c22aecc9S[email protected]                 if (channel->handle != handle) continue;
8592d00edd4Smatthias.ringwald                 hci_con_used = 1;
860*c22aecc9S[email protected]                 break;
861ee091cf1Smatthias.ringwald             }
8622d00edd4Smatthias.ringwald             if (hci_con_used) break;
8632a373862S[email protected]             if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) break;
8649edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
865afde0c52Smatthias.ringwald             break;
866ee091cf1Smatthias.ringwald 
8676e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
868*c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
869*c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
870*c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
871*c22aecc9S[email protected]                 if (!channel->packet_handler) continue;
8726e6710ebSmatthias.ringwald                 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
8736e6710ebSmatthias.ringwald             }
8745652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
8755652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
8765652b5ffS[email protected]             }
8775652b5ffS[email protected]             if (security_protocol_packet_handler) {
878133efcfdSmatthias.ringwald                 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
8795652b5ffS[email protected]             }
8806e6710ebSmatthias.ringwald             break;
8816e6710ebSmatthias.ringwald 
882df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
883df3354fcS[email protected]             handle = READ_BT_16(packet, 3);
884*c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
885*c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
886*c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
887df3354fcS[email protected]                 if (channel->handle != handle) continue;
8882df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
889df3354fcS[email protected]                 break;
890df3354fcS[email protected]             }
891*c22aecc9S[email protected]             break;
892df3354fcS[email protected] 
893a00031e2S[email protected]         case GAP_SECURITY_LEVEL:
894a00031e2S[email protected]             handle = READ_BT_16(packet, 2);
895bd63148eS[email protected]             log_info("l2cap - security level update");
896*c22aecc9S[email protected]             linked_list_iterator_init(&it, &l2cap_channels);
897*c22aecc9S[email protected]             while (linked_list_iterator_has_next(&it)){
898*c22aecc9S[email protected]                 l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
899f85a9399S[email protected]                 if (channel->handle != handle) continue;
9005533f01eS[email protected] 
901bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
902bd63148eS[email protected] 
9035533f01eS[email protected]                 gap_security_level_t actual_level = packet[4];
9045533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
9055533f01eS[email protected] 
906df3354fcS[email protected]                 switch (channel->state){
907df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
9085533f01eS[email protected]                         if (actual_level >= required_level){
909f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
910f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
9111eb2563eS[email protected]                         } else {
9121eb2563eS[email protected]                             channel->reason = 0x03; // security block
9131eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
9141eb2563eS[email protected]                         }
915df3354fcS[email protected]                         break;
916df3354fcS[email protected] 
917df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
9185533f01eS[email protected]                         if (actual_level >= required_level){
919df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
920df3354fcS[email protected]                         } else {
921df3354fcS[email protected]                             // disconnnect, authentication not good enough
922df3354fcS[email protected]                             hci_disconnect_security_block(handle);
923df3354fcS[email protected]                         }
924df3354fcS[email protected]                         break;
925df3354fcS[email protected] 
926df3354fcS[email protected]                     default:
927df3354fcS[email protected]                         break;
928df3354fcS[email protected]                 }
929f85a9399S[email protected]             }
930f85a9399S[email protected]             break;
931f85a9399S[email protected] 
932afde0c52Smatthias.ringwald         default:
933afde0c52Smatthias.ringwald             break;
934afde0c52Smatthias.ringwald     }
935afde0c52Smatthias.ringwald 
936e0707417S[email protected]     // pass on: main packet handler, att and sm packet handlers
937b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
938e0707417S[email protected]     if (attribute_protocol_packet_handler){
939e0707417S[email protected]         (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
940e0707417S[email protected]     }
941e0707417S[email protected]     if (security_protocol_packet_handler) {
942e0707417S[email protected]         (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
943e0707417S[email protected]     }
944bd63148eS[email protected] 
945bd63148eS[email protected]     l2cap_run();
9461e6aba47Smatthias.ringwald }
9471e6aba47Smatthias.ringwald 
948afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
949b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
950e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
951e7ff783cSmatthias.ringwald     l2cap_run();
95284836b65Smatthias.ringwald }
95384836b65Smatthias.ringwald 
9542b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
9554cf56b4aSmatthias.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."
9562b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
9572b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
9582b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
9592b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
9602b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
9612b360848Smatthias.ringwald         signaling_responses_pending++;
9622b360848Smatthias.ringwald         l2cap_run();
9632b360848Smatthias.ringwald     }
9642b360848Smatthias.ringwald }
9652b360848Smatthias.ringwald 
966b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
967645658c9Smatthias.ringwald 
968e0abb8e7S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x\n", handle, psm, source_cid);
969645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
970645658c9Smatthias.ringwald     if (!service) {
971645658c9Smatthias.ringwald         // 0x0002 PSM not supported
9722b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
973645658c9Smatthias.ringwald         return;
974645658c9Smatthias.ringwald     }
975645658c9Smatthias.ringwald 
9765061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
977645658c9Smatthias.ringwald     if (!hci_connection) {
9782b360848Smatthias.ringwald         //
9797d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
980645658c9Smatthias.ringwald         return;
981645658c9Smatthias.ringwald     }
9822bd8b7e7S[email protected] 
9832bd8b7e7S[email protected]     // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP
984b087afb5S[email protected]     if (  hci_ssp_supported_on_both_sides(handle)
985b087afb5S[email protected]     &&    gap_security_level(handle) == LEVEL_0
986b087afb5S[email protected]     &&   !l2cap_security_level_0_allowed_for_PSM(psm)){
9872bd8b7e7S[email protected] 
9882bd8b7e7S[email protected]         // 0x0003 Security Block
9892bd8b7e7S[email protected]         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003);
9902bd8b7e7S[email protected]         return;
9912bd8b7e7S[email protected]     }
9922bd8b7e7S[email protected] 
9932bd8b7e7S[email protected] 
994645658c9Smatthias.ringwald     // alloc structure
9957b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
99628ca2b46S[email protected]     l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
9972b360848Smatthias.ringwald     if (!channel){
9982b360848Smatthias.ringwald         // 0x0004 No resources available
9992b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
10002b360848Smatthias.ringwald         return;
10012b360848Smatthias.ringwald     }
1002645658c9Smatthias.ringwald 
1003645658c9Smatthias.ringwald     // fill in
1004169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
1005169f8b28Smatthias.ringwald     channel->psm = psm;
1006169f8b28Smatthias.ringwald     channel->handle = handle;
1007169f8b28Smatthias.ringwald     channel->connection = service->connection;
1008f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
1009b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
1010b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1011fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
10120a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
1013761b0451Smatthias.ringwald     channel->packets_granted = 0;
1014b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1015df3354fcS[email protected]     channel->required_security_level = service->required_security_level;
1016645658c9Smatthias.ringwald 
10171d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
10182985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
10192985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
10209775e25bSmatthias.ringwald     }
10219775e25bSmatthias.ringwald 
1022645658c9Smatthias.ringwald     // set initial state
1023df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1024ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1025e405ae81Smatthias.ringwald 
1026645658c9Smatthias.ringwald     // add to connections list
1027169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
1028645658c9Smatthias.ringwald 
1029f85a9399S[email protected]     // assert security requirements
10301eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1031e405ae81Smatthias.ringwald }
1032645658c9Smatthias.ringwald 
1033b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
1034e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1035b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1036e405ae81Smatthias.ringwald     if (!channel) {
10377d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
1038e405ae81Smatthias.ringwald         return;
1039e405ae81Smatthias.ringwald     }
1040e405ae81Smatthias.ringwald 
1041552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1042e405ae81Smatthias.ringwald 
1043552d92a1Smatthias.ringwald     // process
1044552d92a1Smatthias.ringwald     l2cap_run();
1045e405ae81Smatthias.ringwald }
1046645658c9Smatthias.ringwald 
1047b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
1048e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1049b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1050e405ae81Smatthias.ringwald     if (!channel) {
10517d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
1052e405ae81Smatthias.ringwald         return;
1053e405ae81Smatthias.ringwald     }
1054e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1055e7ff783cSmatthias.ringwald     channel->reason = reason;
1056e7ff783cSmatthias.ringwald     l2cap_run();
1057645658c9Smatthias.ringwald }
1058645658c9Smatthias.ringwald 
10592784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1060b1988dceSmatthias.ringwald 
1061b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1062b1988dceSmatthias.ringwald 
106363a7246aSmatthias.ringwald     uint16_t flags = READ_BT_16(command, 6);
106463a7246aSmatthias.ringwald     if (flags & 1) {
106563a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
106663a7246aSmatthias.ringwald     }
106763a7246aSmatthias.ringwald 
10682784b77dSmatthias.ringwald     // accept the other's configuration options
10693de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
10703de7c0caSmatthias.ringwald     uint16_t pos     = 8;
10713de7c0caSmatthias.ringwald     while (pos < end_pos){
107263a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
107363a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
107463a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
107563a7246aSmatthias.ringwald         pos++;
10761dc511deSmatthias.ringwald         uint8_t length = command[pos++];
10771dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
107863a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
10791dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
1080e0abb8e7S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
108163a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
108263a7246aSmatthias.ringwald         }
10830fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
10840fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
10850fe7a9d0S[email protected]             channel->flush_timeout = READ_BT_16(command, pos);
10860fe7a9d0S[email protected]         }
108763a7246aSmatthias.ringwald         // check for unknown options
108863a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1089c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
109063a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
10911dc511deSmatthias.ringwald         }
10921dc511deSmatthias.ringwald         pos += length;
10931dc511deSmatthias.ringwald     }
10942784b77dSmatthias.ringwald }
10952784b77dSmatthias.ringwald 
1096fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
10977b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
109873cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
109973cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1100fa8473a4Smatthias.ringwald     return 1;
1101fa8473a4Smatthias.ringwald }
1102fa8473a4Smatthias.ringwald 
1103fa8473a4Smatthias.ringwald 
110400d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11051e6aba47Smatthias.ringwald 
110600d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
110700d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
110838e5900eSmatthias.ringwald     uint16_t result = 0;
11091e6aba47Smatthias.ringwald 
11105842b6d9Smatthias.ringwald     log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
1111b35f641cSmatthias.ringwald 
11129a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
11139a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
11149a011532Smatthias.ringwald         switch (channel->state){
1115fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
11169a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
11172b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
11189a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
11199a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
11209a011532Smatthias.ringwald                 break;
11219a011532Smatthias.ringwald 
11229a011532Smatthias.ringwald             default:
11239a011532Smatthias.ringwald                 // ignore in other states
11249a011532Smatthias.ringwald                 break;
11259a011532Smatthias.ringwald         }
11269a011532Smatthias.ringwald         return;
11279a011532Smatthias.ringwald     }
11289a011532Smatthias.ringwald 
112956081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
11301e6aba47Smatthias.ringwald     switch (channel->state) {
11311e6aba47Smatthias.ringwald 
11321e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
11331e6aba47Smatthias.ringwald             switch (code){
11341e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
11355932bd7cS[email protected]                     l2cap_stop_rtx(channel);
113600d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
113738e5900eSmatthias.ringwald                     switch (result) {
113838e5900eSmatthias.ringwald                         case 0:
1139169f8b28Smatthias.ringwald                             // successful connection
114000d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1141fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
114228ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
114338e5900eSmatthias.ringwald                             break;
114438e5900eSmatthias.ringwald                         case 1:
11455932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
11465932bd7cS[email protected]                             l2cap_start_ertx(channel);
114738e5900eSmatthias.ringwald                             break;
114838e5900eSmatthias.ringwald                         default:
1149eb920dbeSmatthias.ringwald                             // channel closed
1150eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1151f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
115238e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1153eb920dbeSmatthias.ringwald 
1154eb920dbeSmatthias.ringwald                             // drop link key if security block
1155eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
1156eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
1157eb920dbeSmatthias.ringwald                             }
1158eb920dbeSmatthias.ringwald 
1159eb920dbeSmatthias.ringwald                             // discard channel
1160eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1161d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
116238e5900eSmatthias.ringwald                             break;
11631e6aba47Smatthias.ringwald                     }
11641e6aba47Smatthias.ringwald                     break;
116538e5900eSmatthias.ringwald 
116638e5900eSmatthias.ringwald                 default:
11671e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
116838e5900eSmatthias.ringwald                     break;
11691e6aba47Smatthias.ringwald             }
11701e6aba47Smatthias.ringwald             break;
11711e6aba47Smatthias.ringwald 
1172fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1173fe9d8984S[email protected]             result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1174ae280e73Smatthias.ringwald             switch (code) {
1175ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
117628ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1177ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
117863a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
117963a7246aSmatthias.ringwald                         // only done if continuation not set
118063a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
118163a7246aSmatthias.ringwald                     }
1182ae280e73Smatthias.ringwald                     break;
11831e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
11845932bd7cS[email protected]                     l2cap_stop_rtx(channel);
11855932bd7cS[email protected]                     switch (result){
11865932bd7cS[email protected]                         case 0: // success
11875932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
11885932bd7cS[email protected]                             break;
11895932bd7cS[email protected]                         case 4: // pending
11905932bd7cS[email protected]                             l2cap_start_ertx(channel);
11915932bd7cS[email protected]                             break;
11925932bd7cS[email protected]                         default:
1193fe9d8984S[email protected]                             // retry on negative result
1194fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1195fe9d8984S[email protected]                             break;
1196fe9d8984S[email protected]                     }
11975a67bd4aSmatthias.ringwald                     break;
11985a67bd4aSmatthias.ringwald                 default:
11995a67bd4aSmatthias.ringwald                     break;
12001e6aba47Smatthias.ringwald             }
1201fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1202fa8473a4Smatthias.ringwald                 // for open:
12035a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1204fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
12056218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
1206c8e4258aSmatthias.ringwald             }
1207c8e4258aSmatthias.ringwald             break;
1208f62db1e3Smatthias.ringwald 
1209f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1210f62db1e3Smatthias.ringwald             switch (code) {
1211f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
121227a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
121327a923d0Smatthias.ringwald                     break;
12145a67bd4aSmatthias.ringwald                 default:
12155a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
12165a67bd4aSmatthias.ringwald                     break;
121727a923d0Smatthias.ringwald             }
121827a923d0Smatthias.ringwald             break;
121984836b65Smatthias.ringwald 
122084836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
122184836b65Smatthias.ringwald             // @TODO handle incoming requests
122284836b65Smatthias.ringwald             break;
122384836b65Smatthias.ringwald 
122484836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
122584836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
122684836b65Smatthias.ringwald             break;
122710642e45Smatthias.ringwald         default:
122810642e45Smatthias.ringwald             break;
122927a923d0Smatthias.ringwald     }
12307b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
123127a923d0Smatthias.ringwald }
123227a923d0Smatthias.ringwald 
123300d93d79Smatthias.ringwald 
123400d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
123500d93d79Smatthias.ringwald 
123600d93d79Smatthias.ringwald     // get code, signalind identifier and command len
123700d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
123800d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
123900d93d79Smatthias.ringwald 
124000d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
124100d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
124263a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
124300d93d79Smatthias.ringwald         return;
124400d93d79Smatthias.ringwald     }
124500d93d79Smatthias.ringwald 
124600d93d79Smatthias.ringwald     // general commands without an assigned channel
124700d93d79Smatthias.ringwald     switch(code) {
124800d93d79Smatthias.ringwald 
124900d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
125000d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
125100d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
125200d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
12532b83fb7dSmatthias.ringwald             return;
125400d93d79Smatthias.ringwald         }
125500d93d79Smatthias.ringwald 
12562b360848Smatthias.ringwald         case ECHO_REQUEST:
12572b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
12582b83fb7dSmatthias.ringwald             return;
125900d93d79Smatthias.ringwald 
126000d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
126100d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
12622b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
12632b83fb7dSmatthias.ringwald             return;
126400d93d79Smatthias.ringwald         }
126500d93d79Smatthias.ringwald 
126600d93d79Smatthias.ringwald         default:
126700d93d79Smatthias.ringwald             break;
126800d93d79Smatthias.ringwald     }
126900d93d79Smatthias.ringwald 
127000d93d79Smatthias.ringwald 
127100d93d79Smatthias.ringwald     // Get potential destination CID
127200d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
127300d93d79Smatthias.ringwald 
127400d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1275*c22aecc9S[email protected]     linked_list_iterator_t it;
1276*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
1277*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1278*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
1279*c22aecc9S[email protected]         if (channel->handle != handle) continue;
128000d93d79Smatthias.ringwald         if (code & 1) {
1281b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1282b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
128300d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
12844e32727eSmatthias.ringwald                 break;
128500d93d79Smatthias.ringwald             }
128600d93d79Smatthias.ringwald         } else {
1287b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
128800d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
128900d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
12904e32727eSmatthias.ringwald                 break;
129100d93d79Smatthias.ringwald             }
129200d93d79Smatthias.ringwald         }
129300d93d79Smatthias.ringwald     }
129400d93d79Smatthias.ringwald }
129500d93d79Smatthias.ringwald 
129600d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
129700d93d79Smatthias.ringwald 
129800d93d79Smatthias.ringwald     // Get Channel ID
129900d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
130000d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
130100d93d79Smatthias.ringwald 
13025652b5ffS[email protected]     switch (channel_id) {
13035652b5ffS[email protected] 
13045652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13055652b5ffS[email protected] 
130600d93d79Smatthias.ringwald             uint16_t command_offset = 8;
130700d93d79Smatthias.ringwald             while (command_offset < size) {
130800d93d79Smatthias.ringwald 
130900d93d79Smatthias.ringwald                 // handle signaling commands
131000d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
131100d93d79Smatthias.ringwald 
131200d93d79Smatthias.ringwald                 // increment command_offset
131300d93d79Smatthias.ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
131400d93d79Smatthias.ringwald             }
13155652b5ffS[email protected]             break;
131600d93d79Smatthias.ringwald         }
131700d93d79Smatthias.ringwald 
13185652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
13195652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
13205652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13215652b5ffS[email protected]             }
13225652b5ffS[email protected]             break;
13235652b5ffS[email protected] 
13245652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
13255652b5ffS[email protected]             if (security_protocol_packet_handler) {
13265652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
13275652b5ffS[email protected]             }
13285652b5ffS[email protected]             break;
13295652b5ffS[email protected] 
13301bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
13311bbc0b23S[email protected]             // not implemented yet for LE Peripheral
13321bbc0b23S[email protected]             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
13331bbc0b23S[email protected]             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
13341bbc0b23S[email protected]             break;
13351bbc0b23S[email protected]         }
13361bbc0b23S[email protected] 
13375652b5ffS[email protected]         default: {
133800d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
133900d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
134000d93d79Smatthias.ringwald             if (channel) {
134158de5610Smatthias.ringwald                 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
134200d93d79Smatthias.ringwald             }
13435652b5ffS[email protected]             break;
13445652b5ffS[email protected]         }
13455652b5ffS[email protected]     }
134600d93d79Smatthias.ringwald }
134700d93d79Smatthias.ringwald 
13482718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
13492718e2e7Smatthias.ringwald     switch (packet_type) {
13502718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
13512718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
13522718e2e7Smatthias.ringwald             break;
13532718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
13542718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
13552718e2e7Smatthias.ringwald             break;
13562718e2e7Smatthias.ringwald         default:
13572718e2e7Smatthias.ringwald             break;
13582718e2e7Smatthias.ringwald     }
13591eb2563eS[email protected]     l2cap_run();
13602718e2e7Smatthias.ringwald }
136100d93d79Smatthias.ringwald 
136215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
136327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1364f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1365f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1366f62db1e3Smatthias.ringwald     // discard channel
13679dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1368f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1369d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1370c8e4258aSmatthias.ringwald }
13711e6aba47Smatthias.ringwald 
13729d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1373*c22aecc9S[email protected]     linked_list_iterator_t it;
1374*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_services);
1375*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1376*c22aecc9S[email protected]         l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it);
13779d9bbc01Smatthias.ringwald         if ( service->psm == psm){
13789d9bbc01Smatthias.ringwald             return service;
13799d9bbc01Smatthias.ringwald         };
13809d9bbc01Smatthias.ringwald     }
13819d9bbc01Smatthias.ringwald     return NULL;
13829d9bbc01Smatthias.ringwald }
13839d9bbc01Smatthias.ringwald 
138462f901dfS[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){
1385e0abb8e7S[email protected] 
1386e0abb8e7S[email protected]     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1387e0abb8e7S[email protected] 
13884bb582b6Smatthias.ringwald     // check for alread registered psm
13894bb582b6Smatthias.ringwald     // TODO: emit error event
13909d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1391277abc2cSmatthias.ringwald     if (service) {
1392277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
139381476041Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
1394277abc2cSmatthias.ringwald         return;
1395277abc2cSmatthias.ringwald     }
13969d9bbc01Smatthias.ringwald 
13974bb582b6Smatthias.ringwald     // alloc structure
13984bb582b6Smatthias.ringwald     // TODO: emit error event
139928ca2b46S[email protected]     service = (l2cap_service_t *) btstack_memory_l2cap_service_get();
1400277abc2cSmatthias.ringwald     if (!service) {
1401277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
14025842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
1403277abc2cSmatthias.ringwald         return;
1404277abc2cSmatthias.ringwald     }
14059d9bbc01Smatthias.ringwald 
14069d9bbc01Smatthias.ringwald     // fill in
14079d9bbc01Smatthias.ringwald     service->psm = psm;
14089d9bbc01Smatthias.ringwald     service->mtu = mtu;
14099d9bbc01Smatthias.ringwald     service->connection = connection;
1410d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
1411df3354fcS[email protected]     service->required_security_level = security_level;
14129d9bbc01Smatthias.ringwald 
14139d9bbc01Smatthias.ringwald     // add to services list
14149d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
1415c0e866bfSmatthias.ringwald 
1416c0e866bfSmatthias.ringwald     // enable page scan
1417c0e866bfSmatthias.ringwald     hci_connectable_control(1);
14185842b6d9Smatthias.ringwald 
14195842b6d9Smatthias.ringwald     // done
14205842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
14219d9bbc01Smatthias.ringwald }
14229d9bbc01Smatthias.ringwald 
142336944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
1424e0abb8e7S[email protected] 
1425e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1426e0abb8e7S[email protected] 
14279d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1428037d6e48Smatthias.ringwald     if (!service) return;
14299d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1430d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1431c0e866bfSmatthias.ringwald 
1432c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1433c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1434c0e866bfSmatthias.ringwald     hci_connectable_control(0);
14359d9bbc01Smatthias.ringwald }
14369d9bbc01Smatthias.ringwald 
14379d9bbc01Smatthias.ringwald //
143836944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
14399d9bbc01Smatthias.ringwald 
1440*c22aecc9S[email protected]     linked_list_iterator_t it;
1441*c22aecc9S[email protected] 
1442*c22aecc9S[email protected]     // close open channels
1443*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_channels);
1444*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1445*c22aecc9S[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it);
1446*c22aecc9S[email protected]         if (channel->connection != connection) continue;
1447cb8eb7e6Smatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1448c52bf64dSmatthias.ringwald     }
14499d9bbc01Smatthias.ringwald 
1450645658c9Smatthias.ringwald     // unregister services
1451*c22aecc9S[email protected]     linked_list_iterator_init(&it, &l2cap_services);
1452*c22aecc9S[email protected]     while (linked_list_iterator_has_next(&it)){
1453*c22aecc9S[email protected]         l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it);
1454*c22aecc9S[email protected]         if (service->connection != connection) continue;
1455*c22aecc9S[email protected]         linked_list_iterator_remove(&it);
1456d3a9df87Smatthias.ringwald         btstack_memory_l2cap_service_free(service);
1457645658c9Smatthias.ringwald     }
1458cb8eb7e6Smatthias.ringwald 
1459cb8eb7e6Smatthias.ringwald     // process
1460cb8eb7e6Smatthias.ringwald     l2cap_run();
1461c52bf64dSmatthias.ringwald }
14625652b5ffS[email protected] 
14635652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
14645652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
14655652b5ffS[email protected]     switch(channel_id){
14665652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
14675652b5ffS[email protected]             attribute_protocol_packet_handler = packet_handler;
14685652b5ffS[email protected]             break;
14695652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
14705652b5ffS[email protected]             security_protocol_packet_handler = packet_handler;
14715652b5ffS[email protected]             break;
14725652b5ffS[email protected]     }
14735652b5ffS[email protected] }
14745652b5ffS[email protected] 
1475ab2b01dcS[email protected] #ifdef HAVE_BLE
1476ab2b01dcS[email protected] // Request LE connection parameter update
1477ab2b01dcS[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){
14782a373862S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){
1479ab2b01dcS[email protected]         log_info("l2cap_send_signaling_packet, cannot send\n");
1480ab2b01dcS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
1481ab2b01dcS[email protected]     }
1482ab2b01dcS[email protected]     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
1483826f7347S[email protected]     hci_reserve_packet_buffer();
1484facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
1485ab2b01dcS[email protected]     uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier);
1486826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
1487ab2b01dcS[email protected] }
1488ab2b01dcS[email protected] #endif
1489ab2b01dcS[email protected] 
1490