xref: /btstack/src/l2cap.c (revision c177a91c8c75d9830bfffd1801d6bbb8048a366a)
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 
751e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
769d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
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 
805652b5ffS[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler = NULL;
815652b5ffS[email protected] static btstack_packet_handler_t security_protocol_packet_handler = NULL;
825652b5ffS[email protected] 
8339bda6d5Smatthias.ringwald // prototypes
84fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
85fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
86fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
87fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
88fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
89fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
9039bda6d5Smatthias.ringwald 
9139bda6d5Smatthias.ringwald 
921e6aba47Smatthias.ringwald void l2cap_init(){
93808a48abSmatthias.ringwald     new_credits_blocked = 0;
942b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
95808a48abSmatthias.ringwald 
96f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
97f5454fc6Smatthias.ringwald     l2cap_services = NULL;
98f5454fc6Smatthias.ringwald 
99f5454fc6Smatthias.ringwald     packet_handler = null_packet_handler;
100f5454fc6Smatthias.ringwald 
101fcadd0caSmatthias.ringwald     //
1022718e2e7Smatthias.ringwald     // register callback with HCI
103fcadd0caSmatthias.ringwald     //
1042718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
105c0e866bfSmatthias.ringwald     hci_connectable_control(0); // no services yet
106fcadd0caSmatthias.ringwald }
107fcadd0caSmatthias.ringwald 
108fcadd0caSmatthias.ringwald 
109fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
11036944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
111fcadd0caSmatthias.ringwald }
11236944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
113b502e1b0Smatthias.ringwald     packet_handler = handler;
1141e6aba47Smatthias.ringwald }
1151e6aba47Smatthias.ringwald 
11658de5610Smatthias.ringwald //  notify client/protocol handler
11758de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
11858de5610Smatthias.ringwald     if (channel->packet_handler) {
11958de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
12058de5610Smatthias.ringwald     } else {
12136944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
12258de5610Smatthias.ringwald     }
12358de5610Smatthias.ringwald }
12458de5610Smatthias.ringwald 
12558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
126e0abb8e7S[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",
127e0abb8e7S[email protected]              status, bd_addr_to_str(channel->address), channel->handle, channel->psm,
128e0abb8e7S[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
1299893b714Smatthias.ringwald     uint8_t event[21];
13058de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
13158de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13258de5610Smatthias.ringwald     event[2] = status;
13358de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
13458de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
13558de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
13658de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
13758de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1384c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1394c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
14058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
14158de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
14258de5610Smatthias.ringwald }
14358de5610Smatthias.ringwald 
14458de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
145e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
14658de5610Smatthias.ringwald     uint8_t event[4];
14758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
14858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14958de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
15058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15158de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
15258de5610Smatthias.ringwald }
15358de5610Smatthias.ringwald 
15458de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
155e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
156e0abb8e7S[email protected]              bd_addr_to_str(channel->address), channel->handle,  channel->psm, channel->local_cid, channel->remote_cid);
15758de5610Smatthias.ringwald     uint8_t event[16];
15858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
15958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
16058de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
16158de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
16258de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
16358de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
16458de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
16558de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
16658de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1670af41d30Smatthias.ringwald }
168808a48abSmatthias.ringwald 
1695842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
170e0abb8e7S[email protected]     log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm);
1715842b6d9Smatthias.ringwald     uint8_t event[5];
1725842b6d9Smatthias.ringwald     event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
1735842b6d9Smatthias.ringwald     event[1] = sizeof(event) - 2;
1745842b6d9Smatthias.ringwald     event[2] = status;
1755842b6d9Smatthias.ringwald     bt_store_16(event, 3, psm);
1765842b6d9Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1775842b6d9Smatthias.ringwald     (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
1785842b6d9Smatthias.ringwald }
1795842b6d9Smatthias.ringwald 
1806218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
181e0abb8e7S[email protected] 
182e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits);
1836218e6f1Smatthias.ringwald     // track credits
1846218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1856218e6f1Smatthias.ringwald 
1866218e6f1Smatthias.ringwald     uint8_t event[5];
1876218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1886218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1896218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1906218e6f1Smatthias.ringwald     event[4] = credits;
1916218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1926218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1936218e6f1Smatthias.ringwald }
1946218e6f1Smatthias.ringwald 
195808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
196808a48abSmatthias.ringwald     new_credits_blocked = blocked;
197808a48abSmatthias.ringwald }
198808a48abSmatthias.ringwald 
19940d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
200808a48abSmatthias.ringwald 
201808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
202808a48abSmatthias.ringwald 
2038d371091Smatthias.ringwald     linked_item_t *it;
2048d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2058d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
2068d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
2070e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2084c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2098d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2108d371091Smatthias.ringwald         }
2118d371091Smatthias.ringwald     }
2128d371091Smatthias.ringwald }
2138d371091Smatthias.ringwald 
214b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
215f62db1e3Smatthias.ringwald     linked_item_t *it;
216f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2178d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
218b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
219f62db1e3Smatthias.ringwald             return channel;
220f62db1e3Smatthias.ringwald         }
221f62db1e3Smatthias.ringwald     }
222f62db1e3Smatthias.ringwald     return NULL;
223f62db1e3Smatthias.ringwald }
224f62db1e3Smatthias.ringwald 
2256b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2266b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2276b1fde37Smatthias.ringwald     if (!channel) return 0;
2286b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2296b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2306b1fde37Smatthias.ringwald }
2316b1fde37Smatthias.ringwald 
23296cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
23396cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
23496cbd662Smatthias.ringwald     if (channel) {
23596cbd662Smatthias.ringwald         return channel->remote_mtu;
23696cbd662Smatthias.ringwald     }
23796cbd662Smatthias.ringwald     return 0;
23896cbd662Smatthias.ringwald }
23996cbd662Smatthias.ringwald 
24058de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
241b1d43497Smatthias.ringwald 
242b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
243b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
244b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
245b1d43497Smatthias.ringwald     }
246b1d43497Smatthias.ringwald 
2477b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
248b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
24958de5610Smatthias.ringwald     va_list argptr;
25058de5610Smatthias.ringwald     va_start(argptr, identifier);
251816c0598Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
25258de5610Smatthias.ringwald     va_end(argptr);
2537b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
254816c0598Smatthias.ringwald     return hci_send_acl_packet(acl_buffer, len);
25558de5610Smatthias.ringwald }
25658de5610Smatthias.ringwald 
257b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
258b1d43497Smatthias.ringwald     return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
259b1d43497Smatthias.ringwald }
2606218e6f1Smatthias.ringwald 
261b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
262b1d43497Smatthias.ringwald 
263b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
264e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid);
265808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2668ea03fa5Smatthias.ringwald     }
2676218e6f1Smatthias.ringwald 
26858de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
269b1d43497Smatthias.ringwald     if (!channel) {
270e0abb8e7S[email protected]         log_error("l2cap_send_internal no channel for cid 0x%02x\n", local_cid);
271b1d43497Smatthias.ringwald         return -1;   // TODO: define error
2726218e6f1Smatthias.ringwald     }
2736218e6f1Smatthias.ringwald 
274b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
275e0abb8e7S[email protected]         log_error("l2cap_send_internal cid 0x%02x, no credits!\n", local_cid);
276b1d43497Smatthias.ringwald         return -1;  // TODO: define error
277b1d43497Smatthias.ringwald     }
278b1d43497Smatthias.ringwald 
279b1d43497Smatthias.ringwald     --channel->packets_granted;
280b1d43497Smatthias.ringwald 
281e0abb8e7S[email protected]     log_debug("l2cap_send_internal cid 0x%02x, handle %u, 1 credit used, credits left %u;\n",
282b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
283b1d43497Smatthias.ringwald 
284b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
285b1d43497Smatthias.ringwald 
28658de5610Smatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
28758de5610Smatthias.ringwald     bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
28858de5610Smatthias.ringwald     // 2 - ACL length
28958de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
29058de5610Smatthias.ringwald     // 4 - L2CAP packet length
29158de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
29258de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
29358de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
29458de5610Smatthias.ringwald     // send
295b1d43497Smatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
29691b99603Smatthias.ringwald 
29791b99603Smatthias.ringwald     l2cap_hand_out_credits();
29891b99603Smatthias.ringwald 
2996218e6f1Smatthias.ringwald     return err;
30058de5610Smatthias.ringwald }
30158de5610Smatthias.ringwald 
3022149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
3032149f12eSmatthias.ringwald 
3042149f12eSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
305e0abb8e7S[email protected]         log_info("l2cap_send_prepared_to_handle cid 0x%02x, cannot send\n", cid);
3062149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3072149f12eSmatthias.ringwald     }
3082149f12eSmatthias.ringwald 
309e0abb8e7S[email protected]     log_debug("l2cap_send_prepared_to_handle cid 0x%02x, handle %u\n", cid, handle);
3102149f12eSmatthias.ringwald 
3112149f12eSmatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
3122149f12eSmatthias.ringwald 
3132149f12eSmatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
3142149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14));
3152149f12eSmatthias.ringwald     // 2 - ACL length
3162149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
3172149f12eSmatthias.ringwald     // 4 - L2CAP packet length
3182149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
3192149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
3202149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 6, cid);
3212149f12eSmatthias.ringwald     // send
3222149f12eSmatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
3232149f12eSmatthias.ringwald 
3242149f12eSmatthias.ringwald     l2cap_hand_out_credits();
3252149f12eSmatthias.ringwald 
3262149f12eSmatthias.ringwald     return err;
3272149f12eSmatthias.ringwald }
3282149f12eSmatthias.ringwald 
329b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
330b1d43497Smatthias.ringwald 
331b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
332e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid);
333b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
334b1d43497Smatthias.ringwald     }
335b1d43497Smatthias.ringwald 
336b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
337b1d43497Smatthias.ringwald 
338b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
339b1d43497Smatthias.ringwald 
340b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
341b1d43497Smatthias.ringwald }
342b1d43497Smatthias.ringwald 
3432149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
3442149f12eSmatthias.ringwald 
3452149f12eSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
346e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", cid);
3472149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3482149f12eSmatthias.ringwald     }
3492149f12eSmatthias.ringwald 
3502149f12eSmatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
3512149f12eSmatthias.ringwald 
3522149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
3532149f12eSmatthias.ringwald 
3542149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
3552149f12eSmatthias.ringwald }
3562149f12eSmatthias.ringwald 
35728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
35828ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
35928ca2b46S[email protected] }
36028ca2b46S[email protected] 
36128ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
36228ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
36328ca2b46S[email protected] }
36428ca2b46S[email protected] 
36528ca2b46S[email protected] 
366b1d43497Smatthias.ringwald 
3678158c421Smatthias.ringwald // MARK: L2CAP_RUN
3682cd0be45Smatthias.ringwald // process outstanding signaling tasks
3692cd0be45Smatthias.ringwald void l2cap_run(void){
3702b83fb7dSmatthias.ringwald 
3712b83fb7dSmatthias.ringwald     // check pending signaling responses
3722b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
3732b83fb7dSmatthias.ringwald 
37402b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3752b83fb7dSmatthias.ringwald 
3762b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3772b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3782b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
37963a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
3802b83fb7dSmatthias.ringwald 
3812b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3822b360848Smatthias.ringwald             case CONNECTION_REQUEST:
3832b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
3842b360848Smatthias.ringwald                 break;
3852b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3862b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3872b83fb7dSmatthias.ringwald                 break;
3882b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3892b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3902b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3912b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3922b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3932b83fb7dSmatthias.ringwald                 } else {
3942b83fb7dSmatthias.ringwald                     // all other types are not supported
3952b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3962b83fb7dSmatthias.ringwald                 }
3972b83fb7dSmatthias.ringwald                 break;
39863a7246aSmatthias.ringwald             case COMMAND_REJECT:
39963a7246aSmatthias.ringwald                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result);
40063a7246aSmatthias.ringwald                 break;
4012b83fb7dSmatthias.ringwald             default:
4022b83fb7dSmatthias.ringwald                 // should not happen
4032b83fb7dSmatthias.ringwald                 break;
4042b83fb7dSmatthias.ringwald         }
4052b83fb7dSmatthias.ringwald 
4062b83fb7dSmatthias.ringwald         // remove first item
4072b83fb7dSmatthias.ringwald         signaling_responses_pending--;
4082b83fb7dSmatthias.ringwald         int i;
4092b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
41076115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
4112b83fb7dSmatthias.ringwald         }
4122b83fb7dSmatthias.ringwald     }
4132b83fb7dSmatthias.ringwald 
414ae280e73Smatthias.ringwald     uint8_t  config_options[4];
4152cd0be45Smatthias.ringwald     linked_item_t *it;
416756102d3Smatthias.ringwald     linked_item_t *next;
417756102d3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = next){
418756102d3Smatthias.ringwald         next = it->next;    // cache next item as current item might get freed
4192cd0be45Smatthias.ringwald 
42002b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
42102b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
4222cd0be45Smatthias.ringwald 
4232cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
4246e6710ebSmatthias.ringwald 
4257b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
4266e6710ebSmatthias.ringwald 
42756081214Smatthias.ringwald 
4282cd0be45Smatthias.ringwald         switch (channel->state){
4292cd0be45Smatthias.ringwald 
43002b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
43164472d52Smatthias.ringwald                 // send connection request - set state first
43264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
43302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
4348f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
43502b22dc4Smatthias.ringwald                 break;
43602b22dc4Smatthias.ringwald 
437e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
438b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
439e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
440756102d3Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list
441d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
442e7ff783cSmatthias.ringwald                 break;
443e7ff783cSmatthias.ringwald 
444552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
445fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
44628ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
4472a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
448552d92a1Smatthias.ringwald                 break;
449552d92a1Smatthias.ringwald 
4506fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
4516fdcc387Smatthias.ringwald                 // success, start l2cap handshake
452b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4536fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
4542a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
4556fdcc387Smatthias.ringwald                 break;
4566fdcc387Smatthias.ringwald 
457fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
45873cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
45963a7246aSmatthias.ringwald                     uint16_t flags = 0;
46028ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
46163a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
46263a7246aSmatthias.ringwald                         flags = 1;
46363a7246aSmatthias.ringwald                     } else {
46428ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
46563a7246aSmatthias.ringwald                     }
46663a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
46763a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
46863a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
46963a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
47063a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
47163a7246aSmatthias.ringwald                         bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
47263a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
47363a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
47463a7246aSmatthias.ringwald                     } else {
47563a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
47663a7246aSmatthias.ringwald                     }
47763a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
478fa8473a4Smatthias.ringwald                 }
47973cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
48028ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
48128ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
482b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
483ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
484ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
485ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
486b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
487fa8473a4Smatthias.ringwald                 }
488fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
489552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
490552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
491552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
492fa8473a4Smatthias.ringwald                 }
493552d92a1Smatthias.ringwald                 break;
494552d92a1Smatthias.ringwald 
495e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
496b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
497756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
498e7ff783cSmatthias.ringwald                 break;
499e7ff783cSmatthias.ringwald 
500e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
501b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
5022cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
5032a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
5042cd0be45Smatthias.ringwald                 break;
5052cd0be45Smatthias.ringwald             default:
5062cd0be45Smatthias.ringwald                 break;
5072cd0be45Smatthias.ringwald         }
5082cd0be45Smatthias.ringwald     }
5092cd0be45Smatthias.ringwald }
5102cd0be45Smatthias.ringwald 
5114aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
512816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
513fa8c92f6Smatthias.ringwald }
514fa8c92f6Smatthias.ringwald 
5151e6aba47Smatthias.ringwald // open outgoing L2CAP channel
51615470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
51715470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
5181e6aba47Smatthias.ringwald 
519e0abb8e7S[email protected]     log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
520e0abb8e7S[email protected] 
5211e6aba47Smatthias.ringwald     // alloc structure
52228ca2b46S[email protected]     l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
5232b360848Smatthias.ringwald     if (!chan) {
5242b360848Smatthias.ringwald         // emit error event
5252b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
5262b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
5272b360848Smatthias.ringwald         dummy_channel.psm = psm;
5282b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
5292b360848Smatthias.ringwald         return;
5302b360848Smatthias.ringwald     }
5311d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
5322985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
5332985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
5349775e25bSmatthias.ringwald     }
5359775e25bSmatthias.ringwald 
5361e6aba47Smatthias.ringwald     // fill in
5371e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
5381e6aba47Smatthias.ringwald     chan->psm = psm;
5391e6aba47Smatthias.ringwald     chan->handle = 0;
5401e6aba47Smatthias.ringwald     chan->connection = connection;
5416b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
5422784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
54315470d27Smatthias.ringwald     chan->local_mtu = mtu;
5446218e6f1Smatthias.ringwald     chan->packets_granted = 0;
5456218e6f1Smatthias.ringwald 
5461e6aba47Smatthias.ringwald     // set initial state
54702b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
54873cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
549b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
550b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
5511e6aba47Smatthias.ringwald 
5521e6aba47Smatthias.ringwald     // add to connections list
5531e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
5541e6aba47Smatthias.ringwald 
55502b22dc4Smatthias.ringwald     l2cap_run();
55643625864Smatthias.ringwald }
55743625864Smatthias.ringwald 
558b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
559e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
560b35f641cSmatthias.ringwald     // find channel for local_cid
561b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
562f62db1e3Smatthias.ringwald     if (channel) {
563e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
564f62db1e3Smatthias.ringwald     }
5652cd0be45Smatthias.ringwald     // process
5662cd0be45Smatthias.ringwald     l2cap_run();
56743625864Smatthias.ringwald }
5681e6aba47Smatthias.ringwald 
569afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
57015ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
57115ec09bbSmatthias.ringwald     while (it->next){
57215ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
573b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
57402b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
575afde0c52Smatthias.ringwald                 // failure, forward error code
576afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
577afde0c52Smatthias.ringwald                 // discard channel
57815ec09bbSmatthias.ringwald                 it->next = it->next->next;
579d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
580afde0c52Smatthias.ringwald             }
58115ec09bbSmatthias.ringwald         } else {
58215ec09bbSmatthias.ringwald             it = it->next;
583afde0c52Smatthias.ringwald         }
584afde0c52Smatthias.ringwald     }
585afde0c52Smatthias.ringwald }
586afde0c52Smatthias.ringwald 
587afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
588afde0c52Smatthias.ringwald     linked_item_t *it;
589afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
590afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
591afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
59202b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
593b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5946fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
595afde0c52Smatthias.ringwald                 channel->handle = handle;
596b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
597afde0c52Smatthias.ringwald             }
598afde0c52Smatthias.ringwald         }
599afde0c52Smatthias.ringwald     }
6006fdcc387Smatthias.ringwald     // process
6016fdcc387Smatthias.ringwald     l2cap_run();
602afde0c52Smatthias.ringwald }
603b448a0e7Smatthias.ringwald 
604afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
605afde0c52Smatthias.ringwald 
606afde0c52Smatthias.ringwald     bd_addr_t address;
607afde0c52Smatthias.ringwald     hci_con_handle_t handle;
6086e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
60936944dffSmatthias.ringwald     linked_item_t *it;
6102d00edd4Smatthias.ringwald     int hci_con_used;
611afde0c52Smatthias.ringwald 
612afde0c52Smatthias.ringwald     switch(packet[0]){
613afde0c52Smatthias.ringwald 
614afde0c52Smatthias.ringwald         // handle connection complete events
615afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
616afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
617afde0c52Smatthias.ringwald             if (packet[2] == 0){
618afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
619afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
620afde0c52Smatthias.ringwald             } else {
621afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
622afde0c52Smatthias.ringwald             }
623afde0c52Smatthias.ringwald             break;
624afde0c52Smatthias.ringwald 
625afde0c52Smatthias.ringwald         // handle successful create connection cancel command
626afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
627afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
628afde0c52Smatthias.ringwald                 if (packet[5] == 0){
629afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
630afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
631afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
63203cfbabcSmatthias.ringwald                 }
6331e6aba47Smatthias.ringwald             }
63439d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
63539d59809Smatthias.ringwald             break;
63639d59809Smatthias.ringwald 
63739d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
63839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
639afde0c52Smatthias.ringwald             break;
64027a923d0Smatthias.ringwald 
6411e6aba47Smatthias.ringwald         // handle disconnection complete events
642afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
64327a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
644afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
64515ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
64615ec09bbSmatthias.ringwald             while (it->next){
64739ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
64827a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
64915ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
6502060cf14Smatthias.ringwald                     it->next = it->next->next;
65115ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
652d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
65315ec09bbSmatthias.ringwald                 } else {
65415ec09bbSmatthias.ringwald                     it = it->next;
65527a923d0Smatthias.ringwald                 }
65627a923d0Smatthias.ringwald             }
657afde0c52Smatthias.ringwald             break;
658fcadd0caSmatthias.ringwald 
6596218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
66002b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
6618d371091Smatthias.ringwald             l2cap_hand_out_credits();
6626218e6f1Smatthias.ringwald             break;
6636218e6f1Smatthias.ringwald 
664ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
665afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
66636944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
66780ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
6682d00edd4Smatthias.ringwald             hci_con_used = 0;
669ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
670ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
671ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
6722d00edd4Smatthias.ringwald                     hci_con_used = 1;
673ee091cf1Smatthias.ringwald                 }
674ee091cf1Smatthias.ringwald             }
6752d00edd4Smatthias.ringwald             if (hci_con_used) break;
67632ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
6779edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
678afde0c52Smatthias.ringwald             break;
679ee091cf1Smatthias.ringwald 
6806e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
6816e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
6826e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
6836e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
6846e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
6856e6710ebSmatthias.ringwald                 }
6866e6710ebSmatthias.ringwald             }
6875652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
6885652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
6895652b5ffS[email protected]             }
6905652b5ffS[email protected]             if (security_protocol_packet_handler) {
691133efcfdSmatthias.ringwald                 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
6925652b5ffS[email protected]             }
6936e6710ebSmatthias.ringwald             break;
6946e6710ebSmatthias.ringwald 
695afde0c52Smatthias.ringwald         default:
696afde0c52Smatthias.ringwald             break;
697afde0c52Smatthias.ringwald     }
698afde0c52Smatthias.ringwald 
699afde0c52Smatthias.ringwald     // pass on
700b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
7011e6aba47Smatthias.ringwald }
7021e6aba47Smatthias.ringwald 
703afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
704b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
705e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
706e7ff783cSmatthias.ringwald     l2cap_run();
70784836b65Smatthias.ringwald }
70884836b65Smatthias.ringwald 
7092b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
7104cf56b4aSmatthias.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."
7112b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
7122b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
7132b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
7142b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
7152b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
7162b360848Smatthias.ringwald         signaling_responses_pending++;
7172b360848Smatthias.ringwald         l2cap_run();
7182b360848Smatthias.ringwald     }
7192b360848Smatthias.ringwald }
7202b360848Smatthias.ringwald 
721b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
722645658c9Smatthias.ringwald 
723e0abb8e7S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x\n", handle, psm, source_cid);
724645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
725645658c9Smatthias.ringwald     if (!service) {
726645658c9Smatthias.ringwald         // 0x0002 PSM not supported
7272b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
728645658c9Smatthias.ringwald         return;
729645658c9Smatthias.ringwald     }
730645658c9Smatthias.ringwald 
731645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
732645658c9Smatthias.ringwald     if (!hci_connection) {
7332b360848Smatthias.ringwald         //
7347d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
735645658c9Smatthias.ringwald         return;
736645658c9Smatthias.ringwald     }
737645658c9Smatthias.ringwald     // alloc structure
7387b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
73928ca2b46S[email protected]     l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
7402b360848Smatthias.ringwald     if (!channel){
7412b360848Smatthias.ringwald         // 0x0004 No resources available
7422b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
7432b360848Smatthias.ringwald         return;
7442b360848Smatthias.ringwald     }
745645658c9Smatthias.ringwald 
746645658c9Smatthias.ringwald     // fill in
747169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
748169f8b28Smatthias.ringwald     channel->psm = psm;
749169f8b28Smatthias.ringwald     channel->handle = handle;
750169f8b28Smatthias.ringwald     channel->connection = service->connection;
751f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
752b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
753b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
754fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
7550a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
756761b0451Smatthias.ringwald     channel->packets_granted = 0;
757b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
758645658c9Smatthias.ringwald 
7591d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
7602985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
7612985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
7629775e25bSmatthias.ringwald     }
7639775e25bSmatthias.ringwald 
764645658c9Smatthias.ringwald     // set initial state
765e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
76673cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
767e405ae81Smatthias.ringwald 
768645658c9Smatthias.ringwald     // add to connections list
769169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
770645658c9Smatthias.ringwald 
771e405ae81Smatthias.ringwald     // emit incoming connection request
772e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
773e405ae81Smatthias.ringwald }
774645658c9Smatthias.ringwald 
775b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
776e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
777b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
778e405ae81Smatthias.ringwald     if (!channel) {
7797d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
780e405ae81Smatthias.ringwald         return;
781e405ae81Smatthias.ringwald     }
782e405ae81Smatthias.ringwald 
783552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
784e405ae81Smatthias.ringwald 
785552d92a1Smatthias.ringwald     // process
786552d92a1Smatthias.ringwald     l2cap_run();
787e405ae81Smatthias.ringwald }
788645658c9Smatthias.ringwald 
789b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
790e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
791b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
792e405ae81Smatthias.ringwald     if (!channel) {
7937d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
794e405ae81Smatthias.ringwald         return;
795e405ae81Smatthias.ringwald     }
796e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
797e7ff783cSmatthias.ringwald     channel->reason = reason;
798e7ff783cSmatthias.ringwald     l2cap_run();
799645658c9Smatthias.ringwald }
800645658c9Smatthias.ringwald 
8012784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
802b1988dceSmatthias.ringwald 
803b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
804b1988dceSmatthias.ringwald 
80563a7246aSmatthias.ringwald     uint16_t flags = READ_BT_16(command, 6);
80663a7246aSmatthias.ringwald     if (flags & 1) {
80763a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
80863a7246aSmatthias.ringwald     }
80963a7246aSmatthias.ringwald 
8102784b77dSmatthias.ringwald     // accept the other's configuration options
8113de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
8123de7c0caSmatthias.ringwald     uint16_t pos     = 8;
8133de7c0caSmatthias.ringwald     while (pos < end_pos){
81463a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
81563a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
81663a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
81763a7246aSmatthias.ringwald         pos++;
8181dc511deSmatthias.ringwald         uint8_t length = command[pos++];
8191dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
82063a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
8211dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
822e0abb8e7S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
82363a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
82463a7246aSmatthias.ringwald         }
82563a7246aSmatthias.ringwald         // check for unknown options
82663a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
827*c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
82863a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
8291dc511deSmatthias.ringwald         }
8301dc511deSmatthias.ringwald         pos += length;
8311dc511deSmatthias.ringwald     }
8322784b77dSmatthias.ringwald }
8332784b77dSmatthias.ringwald 
834fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
8357b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
83673cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
83773cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
838fa8473a4Smatthias.ringwald     return 1;
839fa8473a4Smatthias.ringwald }
840fa8473a4Smatthias.ringwald 
841fa8473a4Smatthias.ringwald 
84200d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
8431e6aba47Smatthias.ringwald 
84400d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
84500d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
84638e5900eSmatthias.ringwald     uint16_t result = 0;
8471e6aba47Smatthias.ringwald 
8485842b6d9Smatthias.ringwald     log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
849b35f641cSmatthias.ringwald 
8509a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
8519a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
8529a011532Smatthias.ringwald         switch (channel->state){
853fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
8549a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
8552b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
8569a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
8579a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
8589a011532Smatthias.ringwald                 break;
8599a011532Smatthias.ringwald 
8609a011532Smatthias.ringwald             default:
8619a011532Smatthias.ringwald                 // ignore in other states
8629a011532Smatthias.ringwald                 break;
8639a011532Smatthias.ringwald         }
8649a011532Smatthias.ringwald         return;
8659a011532Smatthias.ringwald     }
8669a011532Smatthias.ringwald 
86756081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
8681e6aba47Smatthias.ringwald     switch (channel->state) {
8691e6aba47Smatthias.ringwald 
8701e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
8711e6aba47Smatthias.ringwald             switch (code){
8721e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
87300d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
87438e5900eSmatthias.ringwald                     switch (result) {
87538e5900eSmatthias.ringwald                         case 0:
876169f8b28Smatthias.ringwald                             // successful connection
87700d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
878fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
87928ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
88038e5900eSmatthias.ringwald                             break;
88138e5900eSmatthias.ringwald                         case 1:
88238e5900eSmatthias.ringwald                             // connection pending. get some coffee
88338e5900eSmatthias.ringwald                             break;
88438e5900eSmatthias.ringwald                         default:
885eb920dbeSmatthias.ringwald                             // channel closed
886eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
887eb920dbeSmatthias.ringwald 
888f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
88938e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
890eb920dbeSmatthias.ringwald 
891eb920dbeSmatthias.ringwald                             // drop link key if security block
892eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
893eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
894eb920dbeSmatthias.ringwald                             }
895eb920dbeSmatthias.ringwald 
896eb920dbeSmatthias.ringwald                             // discard channel
897eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
898d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
89938e5900eSmatthias.ringwald                             break;
9001e6aba47Smatthias.ringwald                     }
9011e6aba47Smatthias.ringwald                     break;
90238e5900eSmatthias.ringwald 
90338e5900eSmatthias.ringwald                 default:
9041e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
90538e5900eSmatthias.ringwald                     break;
9061e6aba47Smatthias.ringwald             }
9071e6aba47Smatthias.ringwald             break;
9081e6aba47Smatthias.ringwald 
909fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
910ae280e73Smatthias.ringwald             switch (code) {
911ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
91228ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
913ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
91463a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
91563a7246aSmatthias.ringwald                         // only done if continuation not set
91663a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
91763a7246aSmatthias.ringwald                     }
918ae280e73Smatthias.ringwald                     break;
9191e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
92028ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
9215a67bd4aSmatthias.ringwald                     break;
9225a67bd4aSmatthias.ringwald                 default:
9235a67bd4aSmatthias.ringwald                     break;
9241e6aba47Smatthias.ringwald             }
925fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
926fa8473a4Smatthias.ringwald                 // for open:
9275a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
928fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
9296218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
930c8e4258aSmatthias.ringwald             }
931c8e4258aSmatthias.ringwald             break;
932f62db1e3Smatthias.ringwald 
933f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
934f62db1e3Smatthias.ringwald             switch (code) {
935f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
93627a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
93727a923d0Smatthias.ringwald                     break;
9385a67bd4aSmatthias.ringwald                 default:
9395a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
9405a67bd4aSmatthias.ringwald                     break;
94127a923d0Smatthias.ringwald             }
94227a923d0Smatthias.ringwald             break;
94384836b65Smatthias.ringwald 
94484836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
94584836b65Smatthias.ringwald             // @TODO handle incoming requests
94684836b65Smatthias.ringwald             break;
94784836b65Smatthias.ringwald 
94884836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
94984836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
95084836b65Smatthias.ringwald             break;
95110642e45Smatthias.ringwald         default:
95210642e45Smatthias.ringwald             break;
95327a923d0Smatthias.ringwald     }
9547b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
95527a923d0Smatthias.ringwald }
95627a923d0Smatthias.ringwald 
95700d93d79Smatthias.ringwald 
95800d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
95900d93d79Smatthias.ringwald 
96000d93d79Smatthias.ringwald     // get code, signalind identifier and command len
96100d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
96200d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
96300d93d79Smatthias.ringwald 
96400d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
96500d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
96663a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
96700d93d79Smatthias.ringwald         return;
96800d93d79Smatthias.ringwald     }
96900d93d79Smatthias.ringwald 
97000d93d79Smatthias.ringwald     // general commands without an assigned channel
97100d93d79Smatthias.ringwald     switch(code) {
97200d93d79Smatthias.ringwald 
97300d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
97400d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
97500d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
97600d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
9772b83fb7dSmatthias.ringwald             return;
97800d93d79Smatthias.ringwald         }
97900d93d79Smatthias.ringwald 
9802b360848Smatthias.ringwald         case ECHO_REQUEST:
9812b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
9822b83fb7dSmatthias.ringwald             return;
98300d93d79Smatthias.ringwald 
98400d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
98500d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
9862b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
9872b83fb7dSmatthias.ringwald             return;
98800d93d79Smatthias.ringwald         }
98900d93d79Smatthias.ringwald 
99000d93d79Smatthias.ringwald         default:
99100d93d79Smatthias.ringwald             break;
99200d93d79Smatthias.ringwald     }
99300d93d79Smatthias.ringwald 
99400d93d79Smatthias.ringwald 
99500d93d79Smatthias.ringwald     // Get potential destination CID
99600d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
99700d93d79Smatthias.ringwald 
99800d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
99900d93d79Smatthias.ringwald     linked_item_t *it;
100000d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
100100d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
100200d93d79Smatthias.ringwald         if (channel->handle == handle) {
100300d93d79Smatthias.ringwald             if (code & 1) {
1004b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
1005b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
100600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
10074e32727eSmatthias.ringwald                     break;
100800d93d79Smatthias.ringwald                 }
100900d93d79Smatthias.ringwald             } else {
1010b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
101100d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
101200d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
10134e32727eSmatthias.ringwald                     break;
101400d93d79Smatthias.ringwald                 }
101500d93d79Smatthias.ringwald             }
101600d93d79Smatthias.ringwald         }
101700d93d79Smatthias.ringwald     }
101800d93d79Smatthias.ringwald }
101900d93d79Smatthias.ringwald 
102000d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
102100d93d79Smatthias.ringwald 
102200d93d79Smatthias.ringwald     // Get Channel ID
102300d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
102400d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
102500d93d79Smatthias.ringwald 
10265652b5ffS[email protected]     switch (channel_id) {
10275652b5ffS[email protected] 
10285652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
10295652b5ffS[email protected] 
103000d93d79Smatthias.ringwald             uint16_t command_offset = 8;
103100d93d79Smatthias.ringwald             while (command_offset < size) {
103200d93d79Smatthias.ringwald 
103300d93d79Smatthias.ringwald                 // handle signaling commands
103400d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
103500d93d79Smatthias.ringwald 
103600d93d79Smatthias.ringwald                 // increment command_offset
103700d93d79Smatthias.ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
103800d93d79Smatthias.ringwald             }
10395652b5ffS[email protected]             break;
104000d93d79Smatthias.ringwald         }
104100d93d79Smatthias.ringwald 
10425652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
10435652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
10445652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
10455652b5ffS[email protected]             }
10465652b5ffS[email protected]             break;
10475652b5ffS[email protected] 
10485652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
10495652b5ffS[email protected]             if (security_protocol_packet_handler) {
10505652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
10515652b5ffS[email protected]             }
10525652b5ffS[email protected]             break;
10535652b5ffS[email protected] 
10545652b5ffS[email protected]         default: {
105500d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
105600d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
105700d93d79Smatthias.ringwald             if (channel) {
105858de5610Smatthias.ringwald                 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
105900d93d79Smatthias.ringwald             }
10605652b5ffS[email protected]             break;
10615652b5ffS[email protected]         }
10625652b5ffS[email protected]     }
10635652b5ffS[email protected] 
10645652b5ffS[email protected]     l2cap_run();
106500d93d79Smatthias.ringwald }
106600d93d79Smatthias.ringwald 
10672718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
10682718e2e7Smatthias.ringwald     switch (packet_type) {
10692718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
10702718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
10712718e2e7Smatthias.ringwald             break;
10722718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
10732718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
10742718e2e7Smatthias.ringwald             break;
10752718e2e7Smatthias.ringwald         default:
10762718e2e7Smatthias.ringwald             break;
10772718e2e7Smatthias.ringwald     }
10782718e2e7Smatthias.ringwald }
107900d93d79Smatthias.ringwald 
108015ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
108127a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1082f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1083f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1084f62db1e3Smatthias.ringwald     // discard channel
1085f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1086d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1087c8e4258aSmatthias.ringwald }
10881e6aba47Smatthias.ringwald 
10899d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1090c52bf64dSmatthias.ringwald     linked_item_t *it;
10919d9bbc01Smatthias.ringwald 
10929d9bbc01Smatthias.ringwald     // close open channels
10939d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
10949d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
10959d9bbc01Smatthias.ringwald         if ( service->psm == psm){
10969d9bbc01Smatthias.ringwald             return service;
10979d9bbc01Smatthias.ringwald         };
10989d9bbc01Smatthias.ringwald     }
10999d9bbc01Smatthias.ringwald     return NULL;
11009d9bbc01Smatthias.ringwald }
11019d9bbc01Smatthias.ringwald 
110236944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
1103e0abb8e7S[email protected] 
1104e0abb8e7S[email protected]     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1105e0abb8e7S[email protected] 
11064bb582b6Smatthias.ringwald     // check for alread registered psm
11074bb582b6Smatthias.ringwald     // TODO: emit error event
11089d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1109277abc2cSmatthias.ringwald     if (service) {
1110277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
111181476041Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
1112277abc2cSmatthias.ringwald         return;
1113277abc2cSmatthias.ringwald     }
11149d9bbc01Smatthias.ringwald 
11154bb582b6Smatthias.ringwald     // alloc structure
11164bb582b6Smatthias.ringwald     // TODO: emit error event
111728ca2b46S[email protected]     service = (l2cap_service_t *) btstack_memory_l2cap_service_get();
1118277abc2cSmatthias.ringwald     if (!service) {
1119277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
11205842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
1121277abc2cSmatthias.ringwald         return;
1122277abc2cSmatthias.ringwald     }
11239d9bbc01Smatthias.ringwald 
11249d9bbc01Smatthias.ringwald     // fill in
11259d9bbc01Smatthias.ringwald     service->psm = psm;
11269d9bbc01Smatthias.ringwald     service->mtu = mtu;
11279d9bbc01Smatthias.ringwald     service->connection = connection;
1128d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
11299d9bbc01Smatthias.ringwald 
11309d9bbc01Smatthias.ringwald     // add to services list
11319d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
1132c0e866bfSmatthias.ringwald 
1133c0e866bfSmatthias.ringwald     // enable page scan
1134c0e866bfSmatthias.ringwald     hci_connectable_control(1);
11355842b6d9Smatthias.ringwald 
11365842b6d9Smatthias.ringwald     // done
11375842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
11389d9bbc01Smatthias.ringwald }
11399d9bbc01Smatthias.ringwald 
114036944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
1141e0abb8e7S[email protected] 
1142e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1143e0abb8e7S[email protected] 
11449d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1145037d6e48Smatthias.ringwald     if (!service) return;
11469d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1147d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1148c0e866bfSmatthias.ringwald 
1149c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1150c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1151c0e866bfSmatthias.ringwald     hci_connectable_control(0);
11529d9bbc01Smatthias.ringwald }
11539d9bbc01Smatthias.ringwald 
11549d9bbc01Smatthias.ringwald //
115536944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
11569d9bbc01Smatthias.ringwald     linked_item_t *it;
11579d9bbc01Smatthias.ringwald 
11583a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1159c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1160c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1161c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1162c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1163cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1164c52bf64dSmatthias.ringwald         }
1165c52bf64dSmatthias.ringwald     }
11669d9bbc01Smatthias.ringwald 
1167645658c9Smatthias.ringwald     // unregister services
116869025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
116969025de8Smatthias.ringwald     while (it->next) {
117069025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1171645658c9Smatthias.ringwald         if (service->connection == connection){
117269025de8Smatthias.ringwald             it->next = it->next->next;
1173d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
11748149d2c7Smatthias.ringwald         } else {
11758149d2c7Smatthias.ringwald             it = it->next;
1176645658c9Smatthias.ringwald         }
1177645658c9Smatthias.ringwald     }
1178cb8eb7e6Smatthias.ringwald 
1179cb8eb7e6Smatthias.ringwald     // process
1180cb8eb7e6Smatthias.ringwald     l2cap_run();
1181c52bf64dSmatthias.ringwald }
11825652b5ffS[email protected] 
11835652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
11845652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
11855652b5ffS[email protected]     switch(channel_id){
11865652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
11875652b5ffS[email protected]             attribute_protocol_packet_handler = packet_handler;
11885652b5ffS[email protected]             break;
11895652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
11905652b5ffS[email protected]             security_protocol_packet_handler = packet_handler;
11915652b5ffS[email protected]             break;
11925652b5ffS[email protected]     }
11935652b5ffS[email protected] }
11945652b5ffS[email protected] 
1195