xref: /btstack/src/l2cap.c (revision b1d43497a558ff7325db97cb370c09308c2c93e4)
143625864Smatthias.ringwald /*
21713bceaSmatthias.ringwald  * Copyright (C) 2009 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.
161713bceaSmatthias.ringwald  *
171713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
181713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
211713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
231713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
241713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
271713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281713bceaSmatthias.ringwald  * SUCH DAMAGE.
291713bceaSmatthias.ringwald  *
301713bceaSmatthias.ringwald  */
311713bceaSmatthias.ringwald 
321713bceaSmatthias.ringwald /*
3343625864Smatthias.ringwald  *  l2cap.c
3443625864Smatthias.ringwald  *
3543625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
3643625864Smatthias.ringwald  *
3743625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
3843625864Smatthias.ringwald  */
3943625864Smatthias.ringwald 
4043625864Smatthias.ringwald #include "l2cap.h"
41645658c9Smatthias.ringwald #include "hci.h"
422b3c6c9bSmatthias.ringwald #include "hci_dump.h"
436218e6f1Smatthias.ringwald #include "debug.h"
44d3a9df87Smatthias.ringwald #include "btstack_memory.h"
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include <stdarg.h>
4743625864Smatthias.ringwald #include <string.h>
4843625864Smatthias.ringwald 
4943625864Smatthias.ringwald #include <stdio.h>
5043625864Smatthias.ringwald 
51ea54feb7Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets (8)
5236a5e735Smatthias.ringwald #define COMPLETE_L2CAP_HEADER (HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE)
536f60b3f4Smatthias.ringwald 
542784b77dSmatthias.ringwald // minimum signaling MTU
552784b77dSmatthias.ringwald #define L2CAP_MINIMAL_MTU 48
561dc511deSmatthias.ringwald #define L2CAP_DEFAULT_MTU 672
572784b77dSmatthias.ringwald 
584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
604c744e21Smatthias.ringwald 
6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
622b360848Smatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 5
6339bda6d5Smatthias.ringwald 
6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6900d93d79Smatthias.ringwald 
7039bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
7139bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
7239bda6d5Smatthias.ringwald 
7339bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
742b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
752b83fb7dSmatthias.ringwald static int signaling_responses_pending;
762b83fb7dSmatthias.ringwald 
771e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
789d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
7936944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
80808a48abSmatthias.ringwald static int new_credits_blocked = 0;
811e6aba47Smatthias.ringwald 
8239bda6d5Smatthias.ringwald // prototypes
83fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
84fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
85fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
86fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
87fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
88fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8939bda6d5Smatthias.ringwald 
9039bda6d5Smatthias.ringwald 
911e6aba47Smatthias.ringwald void l2cap_init(){
92fcadd0caSmatthias.ringwald 
93808a48abSmatthias.ringwald     new_credits_blocked = 0;
942b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
95808a48abSmatthias.ringwald 
96fcadd0caSmatthias.ringwald     //
972718e2e7Smatthias.ringwald     // register callback with HCI
98fcadd0caSmatthias.ringwald     //
992718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
100fcadd0caSmatthias.ringwald }
101fcadd0caSmatthias.ringwald 
102fcadd0caSmatthias.ringwald 
103fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
10436944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
105fcadd0caSmatthias.ringwald }
10636944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
107b502e1b0Smatthias.ringwald     packet_handler = handler;
1081e6aba47Smatthias.ringwald }
1091e6aba47Smatthias.ringwald 
11058de5610Smatthias.ringwald //  notify client/protocol handler
11158de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
11258de5610Smatthias.ringwald     if (channel->packet_handler) {
11358de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
11458de5610Smatthias.ringwald     } else {
11536944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
11658de5610Smatthias.ringwald     }
11758de5610Smatthias.ringwald }
11858de5610Smatthias.ringwald 
11958de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1209893b714Smatthias.ringwald     uint8_t event[21];
12158de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
12258de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12358de5610Smatthias.ringwald     event[2] = status;
12458de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
12558de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
12658de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
12758de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
12858de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1294c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1304c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
13158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13358de5610Smatthias.ringwald }
13458de5610Smatthias.ringwald 
13558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
13658de5610Smatthias.ringwald     uint8_t event[4];
13758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
13858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13958de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
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_connection_request(l2cap_channel_t *channel) {
14558de5610Smatthias.ringwald     uint8_t event[16];
14658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
14758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14858de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
14958de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
15058de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
15158de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
15258de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
15358de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15458de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1550af41d30Smatthias.ringwald }
156808a48abSmatthias.ringwald 
1576218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1586218e6f1Smatthias.ringwald     // track credits
1596218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1607b5fbe1fSmatthias.ringwald     // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1616218e6f1Smatthias.ringwald 
1626218e6f1Smatthias.ringwald     uint8_t event[5];
1636218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1646218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1656218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1666218e6f1Smatthias.ringwald     event[4] = credits;
1676218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1686218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1696218e6f1Smatthias.ringwald }
1706218e6f1Smatthias.ringwald 
171808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
172808a48abSmatthias.ringwald     new_credits_blocked = blocked;
173808a48abSmatthias.ringwald }
174808a48abSmatthias.ringwald 
17540d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
176808a48abSmatthias.ringwald 
177808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
178808a48abSmatthias.ringwald 
1798d371091Smatthias.ringwald     linked_item_t *it;
1808d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1818d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1828d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1830e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1844c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1858d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1868d371091Smatthias.ringwald         }
1878d371091Smatthias.ringwald     }
1888d371091Smatthias.ringwald }
1898d371091Smatthias.ringwald 
190b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
191f62db1e3Smatthias.ringwald     linked_item_t *it;
192f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1938d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
194b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
195f62db1e3Smatthias.ringwald             return channel;
196f62db1e3Smatthias.ringwald         }
197f62db1e3Smatthias.ringwald     }
198f62db1e3Smatthias.ringwald     return NULL;
199f62db1e3Smatthias.ringwald }
200f62db1e3Smatthias.ringwald 
2016b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2026b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2036b1fde37Smatthias.ringwald     if (!channel) return 0;
2046b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2056b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2066b1fde37Smatthias.ringwald }
2076b1fde37Smatthias.ringwald 
20896cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
20996cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
21096cbd662Smatthias.ringwald     if (channel) {
21196cbd662Smatthias.ringwald         return channel->remote_mtu;
21296cbd662Smatthias.ringwald     }
21396cbd662Smatthias.ringwald     return 0;
21496cbd662Smatthias.ringwald }
21596cbd662Smatthias.ringwald 
21658de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
217*b1d43497Smatthias.ringwald 
218*b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
219*b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
220*b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
221*b1d43497Smatthias.ringwald     }
222*b1d43497Smatthias.ringwald 
2237b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
224*b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
22558de5610Smatthias.ringwald     va_list argptr;
22658de5610Smatthias.ringwald     va_start(argptr, identifier);
227816c0598Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
22858de5610Smatthias.ringwald     va_end(argptr);
2297b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
230816c0598Smatthias.ringwald     return hci_send_acl_packet(acl_buffer, len);
23158de5610Smatthias.ringwald }
23258de5610Smatthias.ringwald 
233*b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
234*b1d43497Smatthias.ringwald     return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
235*b1d43497Smatthias.ringwald }
2366218e6f1Smatthias.ringwald 
237*b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
238*b1d43497Smatthias.ringwald 
239*b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
240*b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
241808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2428ea03fa5Smatthias.ringwald     }
2436218e6f1Smatthias.ringwald 
24458de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
245*b1d43497Smatthias.ringwald     if (!channel) {
246*b1d43497Smatthias.ringwald         log_error("l2cap_send_internal no channel for cid %u\n", local_cid);
247*b1d43497Smatthias.ringwald         return -1;   // TODO: define error
2486218e6f1Smatthias.ringwald     }
2496218e6f1Smatthias.ringwald 
250*b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
251*b1d43497Smatthias.ringwald         log_error("l2cap_send_internal cid %u, no credits!\n", local_cid);
252*b1d43497Smatthias.ringwald         return -1;  // TODO: define error
253*b1d43497Smatthias.ringwald     }
254*b1d43497Smatthias.ringwald 
255*b1d43497Smatthias.ringwald     --channel->packets_granted;
256*b1d43497Smatthias.ringwald 
257*b1d43497Smatthias.ringwald     log_debug("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
258*b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
259*b1d43497Smatthias.ringwald 
260*b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
261*b1d43497Smatthias.ringwald 
26258de5610Smatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
26358de5610Smatthias.ringwald     bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
26458de5610Smatthias.ringwald     // 2 - ACL length
26558de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
26658de5610Smatthias.ringwald     // 4 - L2CAP packet length
26758de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
26858de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
26958de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
27058de5610Smatthias.ringwald     // send
271*b1d43497Smatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
27291b99603Smatthias.ringwald 
27391b99603Smatthias.ringwald     l2cap_hand_out_credits();
27491b99603Smatthias.ringwald 
2756218e6f1Smatthias.ringwald     return err;
27658de5610Smatthias.ringwald }
27758de5610Smatthias.ringwald 
278*b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
279*b1d43497Smatthias.ringwald 
280*b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
281*b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
282*b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
283*b1d43497Smatthias.ringwald     }
284*b1d43497Smatthias.ringwald 
285*b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
286*b1d43497Smatthias.ringwald 
287*b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
288*b1d43497Smatthias.ringwald 
289*b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
290*b1d43497Smatthias.ringwald }
291*b1d43497Smatthias.ringwald 
292*b1d43497Smatthias.ringwald 
2938158c421Smatthias.ringwald // MARK: L2CAP_RUN
2942cd0be45Smatthias.ringwald // process outstanding signaling tasks
2952cd0be45Smatthias.ringwald void l2cap_run(void){
2962b83fb7dSmatthias.ringwald 
2972b83fb7dSmatthias.ringwald     // check pending signaling responses
2982b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
2992b83fb7dSmatthias.ringwald 
30002b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3012b83fb7dSmatthias.ringwald 
3022b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3032b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3042b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
3052b360848Smatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST
3062b83fb7dSmatthias.ringwald 
3072b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3082b360848Smatthias.ringwald             case CONNECTION_REQUEST:
3092b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
3102b360848Smatthias.ringwald                 break;
3112b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3122b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3132b83fb7dSmatthias.ringwald                 break;
3142b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3152b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3162b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3172b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3182b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3192b83fb7dSmatthias.ringwald                 } else {
3202b83fb7dSmatthias.ringwald                     // all other types are not supported
3212b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3222b83fb7dSmatthias.ringwald                 }
3232b83fb7dSmatthias.ringwald                 break;
3242b83fb7dSmatthias.ringwald             default:
3252b83fb7dSmatthias.ringwald                 // should not happen
3262b83fb7dSmatthias.ringwald                 break;
3272b83fb7dSmatthias.ringwald         }
3282b83fb7dSmatthias.ringwald 
3292b83fb7dSmatthias.ringwald         // remove first item
3302b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3312b83fb7dSmatthias.ringwald         int i;
3322b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
3332b83fb7dSmatthias.ringwald             signaling_responses[i] = signaling_responses[i+1];
3342b83fb7dSmatthias.ringwald         }
3352b83fb7dSmatthias.ringwald     }
3362b83fb7dSmatthias.ringwald 
337ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3382cd0be45Smatthias.ringwald     linked_item_t *it;
3392cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3402cd0be45Smatthias.ringwald 
34102b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
34202b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3432cd0be45Smatthias.ringwald 
3442cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
3456e6710ebSmatthias.ringwald 
3467b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
3476e6710ebSmatthias.ringwald 
3482cd0be45Smatthias.ringwald         switch (channel->state){
3492cd0be45Smatthias.ringwald 
35002b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
35164472d52Smatthias.ringwald                 // send connection request - set state first
35264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
35302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
3548f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
35502b22dc4Smatthias.ringwald                 break;
35602b22dc4Smatthias.ringwald 
357e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
358b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
359e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
360e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
361d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
362e7ff783cSmatthias.ringwald                 break;
363e7ff783cSmatthias.ringwald 
364552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
365fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
36673cf2b3dSmatthias.ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
3672a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
368552d92a1Smatthias.ringwald                 break;
369552d92a1Smatthias.ringwald 
3706fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
3716fdcc387Smatthias.ringwald                 // success, start l2cap handshake
372b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
3736fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
3742a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
3756fdcc387Smatthias.ringwald                 break;
3766fdcc387Smatthias.ringwald 
377fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
37873cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
37973cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
38073cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP;
381fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
382fa8473a4Smatthias.ringwald                 }
38373cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
38473cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
38573cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ;
386b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
387ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
388ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
389ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
390b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
391fa8473a4Smatthias.ringwald                 }
392fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
393552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
394552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
395552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
396fa8473a4Smatthias.ringwald                 }
397552d92a1Smatthias.ringwald                 break;
398552d92a1Smatthias.ringwald 
399e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
400b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
401e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
402e7ff783cSmatthias.ringwald                 break;
403e7ff783cSmatthias.ringwald 
404e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
405b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4062cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4072a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4082cd0be45Smatthias.ringwald                 break;
4092cd0be45Smatthias.ringwald             default:
4102cd0be45Smatthias.ringwald                 break;
4112cd0be45Smatthias.ringwald         }
4122cd0be45Smatthias.ringwald     }
4132cd0be45Smatthias.ringwald }
4142cd0be45Smatthias.ringwald 
415fa8c92f6Smatthias.ringwald static uint16_t l2cap_max_l2cap_mtu(void){
416816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
417fa8c92f6Smatthias.ringwald }
418fa8c92f6Smatthias.ringwald 
4191e6aba47Smatthias.ringwald // open outgoing L2CAP channel
42015470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
42115470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4221e6aba47Smatthias.ringwald 
4231e6aba47Smatthias.ringwald     // alloc structure
424d3a9df87Smatthias.ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
4252b360848Smatthias.ringwald     if (!chan) {
4262b360848Smatthias.ringwald         // emit error event
4272b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
4282b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
4292b360848Smatthias.ringwald         dummy_channel.psm = psm;
4302b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
4312b360848Smatthias.ringwald         return;
4322b360848Smatthias.ringwald     }
4331d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
434fa8c92f6Smatthias.ringwald     if (mtu > l2cap_max_l2cap_mtu()) {
435fa8c92f6Smatthias.ringwald         mtu = l2cap_max_l2cap_mtu();
4369775e25bSmatthias.ringwald     }
4379775e25bSmatthias.ringwald 
4381e6aba47Smatthias.ringwald     // fill in
4391e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4401e6aba47Smatthias.ringwald     chan->psm = psm;
4411e6aba47Smatthias.ringwald     chan->handle = 0;
4421e6aba47Smatthias.ringwald     chan->connection = connection;
4436b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4442784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
44515470d27Smatthias.ringwald     chan->local_mtu = mtu;
4466218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4476218e6f1Smatthias.ringwald 
4481e6aba47Smatthias.ringwald     // set initial state
44902b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
45073cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
451b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
452b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
4531e6aba47Smatthias.ringwald 
4541e6aba47Smatthias.ringwald     // add to connections list
4551e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
4561e6aba47Smatthias.ringwald 
45702b22dc4Smatthias.ringwald     l2cap_run();
45843625864Smatthias.ringwald }
45943625864Smatthias.ringwald 
460b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
461b35f641cSmatthias.ringwald     // find channel for local_cid
462b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
463f62db1e3Smatthias.ringwald     if (channel) {
464e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
465f62db1e3Smatthias.ringwald     }
4662cd0be45Smatthias.ringwald     // process
4672cd0be45Smatthias.ringwald     l2cap_run();
46843625864Smatthias.ringwald }
4691e6aba47Smatthias.ringwald 
470afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
47115ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
47215ec09bbSmatthias.ringwald     while (it->next){
47315ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
474b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
47502b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
476afde0c52Smatthias.ringwald                 // failure, forward error code
477afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
478afde0c52Smatthias.ringwald                 // discard channel
47915ec09bbSmatthias.ringwald                 it->next = it->next->next;
480d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
481afde0c52Smatthias.ringwald             }
48215ec09bbSmatthias.ringwald         } else {
48315ec09bbSmatthias.ringwald             it = it->next;
484afde0c52Smatthias.ringwald         }
485afde0c52Smatthias.ringwald     }
486afde0c52Smatthias.ringwald }
487afde0c52Smatthias.ringwald 
488afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
489afde0c52Smatthias.ringwald     linked_item_t *it;
490afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
491afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
492afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
49302b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
494b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
4956fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
496afde0c52Smatthias.ringwald                 channel->handle = handle;
497b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
498afde0c52Smatthias.ringwald             }
499afde0c52Smatthias.ringwald         }
500afde0c52Smatthias.ringwald     }
5016fdcc387Smatthias.ringwald     // process
5026fdcc387Smatthias.ringwald     l2cap_run();
503afde0c52Smatthias.ringwald }
504b448a0e7Smatthias.ringwald 
505afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
506afde0c52Smatthias.ringwald 
507afde0c52Smatthias.ringwald     bd_addr_t address;
508afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5096e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
51036944dffSmatthias.ringwald     linked_item_t *it;
5112d00edd4Smatthias.ringwald     int hci_con_used;
512afde0c52Smatthias.ringwald 
513afde0c52Smatthias.ringwald     switch(packet[0]){
514afde0c52Smatthias.ringwald 
515afde0c52Smatthias.ringwald         // handle connection complete events
516afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
517afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
518afde0c52Smatthias.ringwald             if (packet[2] == 0){
519afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
520afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
521afde0c52Smatthias.ringwald             } else {
522afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
523afde0c52Smatthias.ringwald             }
524afde0c52Smatthias.ringwald             break;
525afde0c52Smatthias.ringwald 
526afde0c52Smatthias.ringwald         // handle successful create connection cancel command
527afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
528afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
529afde0c52Smatthias.ringwald                 if (packet[5] == 0){
530afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
531afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
532afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
53303cfbabcSmatthias.ringwald                 }
5341e6aba47Smatthias.ringwald             }
53539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
53639d59809Smatthias.ringwald             break;
53739d59809Smatthias.ringwald 
53839d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
53939d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
540afde0c52Smatthias.ringwald             break;
54127a923d0Smatthias.ringwald 
5421e6aba47Smatthias.ringwald         // handle disconnection complete events
543afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
54427a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
545afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
54615ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
54715ec09bbSmatthias.ringwald             while (it->next){
54839ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
54927a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
55015ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
5512060cf14Smatthias.ringwald                     it->next = it->next->next;
55215ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
553d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
55415ec09bbSmatthias.ringwald                 } else {
55515ec09bbSmatthias.ringwald                     it = it->next;
55627a923d0Smatthias.ringwald                 }
55727a923d0Smatthias.ringwald             }
558afde0c52Smatthias.ringwald             break;
559fcadd0caSmatthias.ringwald 
5606218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
56102b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
5628d371091Smatthias.ringwald             l2cap_hand_out_credits();
5636218e6f1Smatthias.ringwald             break;
5646218e6f1Smatthias.ringwald 
565ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
566afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
56736944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
56880ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
5692d00edd4Smatthias.ringwald             hci_con_used = 0;
570ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
571ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
572ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
5732d00edd4Smatthias.ringwald                     hci_con_used = 1;
574ee091cf1Smatthias.ringwald                 }
575ee091cf1Smatthias.ringwald             }
5762d00edd4Smatthias.ringwald             if (hci_con_used) break;
57732ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
5789edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
579afde0c52Smatthias.ringwald             break;
580ee091cf1Smatthias.ringwald 
5816e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
5826e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
5836e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
5846e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
5856e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
5866e6710ebSmatthias.ringwald                 }
5876e6710ebSmatthias.ringwald             }
5886e6710ebSmatthias.ringwald             break;
5896e6710ebSmatthias.ringwald 
590afde0c52Smatthias.ringwald         default:
591afde0c52Smatthias.ringwald             break;
592afde0c52Smatthias.ringwald     }
593afde0c52Smatthias.ringwald 
594afde0c52Smatthias.ringwald     // pass on
595b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
5961e6aba47Smatthias.ringwald }
5971e6aba47Smatthias.ringwald 
598afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
599b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
600e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
601e7ff783cSmatthias.ringwald     l2cap_run();
60284836b65Smatthias.ringwald }
60384836b65Smatthias.ringwald 
6042b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
6052b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
6062b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
6072b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
6082b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
6092b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
6102b360848Smatthias.ringwald         signaling_responses_pending++;
6112b360848Smatthias.ringwald         l2cap_run();
6122b360848Smatthias.ringwald     }
6132b360848Smatthias.ringwald }
6142b360848Smatthias.ringwald 
615b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
616645658c9Smatthias.ringwald 
6177b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
618645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
619645658c9Smatthias.ringwald     if (!service) {
620645658c9Smatthias.ringwald         // 0x0002 PSM not supported
6212b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
622645658c9Smatthias.ringwald         return;
623645658c9Smatthias.ringwald     }
624645658c9Smatthias.ringwald 
625645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
626645658c9Smatthias.ringwald     if (!hci_connection) {
6272b360848Smatthias.ringwald         //
6287d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
629645658c9Smatthias.ringwald         return;
630645658c9Smatthias.ringwald     }
631645658c9Smatthias.ringwald     // alloc structure
6327b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
633d3a9df87Smatthias.ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
6342b360848Smatthias.ringwald     if (!channel){
6352b360848Smatthias.ringwald         // 0x0004 No resources available
6362b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
6372b360848Smatthias.ringwald         return;
6382b360848Smatthias.ringwald     }
639645658c9Smatthias.ringwald 
640645658c9Smatthias.ringwald     // fill in
641169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
642169f8b28Smatthias.ringwald     channel->psm = psm;
643169f8b28Smatthias.ringwald     channel->handle = handle;
644169f8b28Smatthias.ringwald     channel->connection = service->connection;
645f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
646b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
647b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
648fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6490a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
650761b0451Smatthias.ringwald     channel->packets_granted = 0;
651b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
652645658c9Smatthias.ringwald 
6531d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
654fa8c92f6Smatthias.ringwald     if (channel->local_mtu > l2cap_max_l2cap_mtu()) {
655fa8c92f6Smatthias.ringwald         channel->local_mtu = l2cap_max_l2cap_mtu();
6569775e25bSmatthias.ringwald     }
6579775e25bSmatthias.ringwald 
658645658c9Smatthias.ringwald     // set initial state
659e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
66073cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
661e405ae81Smatthias.ringwald 
662645658c9Smatthias.ringwald     // add to connections list
663169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
664645658c9Smatthias.ringwald 
665e405ae81Smatthias.ringwald     // emit incoming connection request
666e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
667e405ae81Smatthias.ringwald }
668645658c9Smatthias.ringwald 
669b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
670b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
671e405ae81Smatthias.ringwald     if (!channel) {
6727d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
673e405ae81Smatthias.ringwald         return;
674e405ae81Smatthias.ringwald     }
675e405ae81Smatthias.ringwald 
676552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
677e405ae81Smatthias.ringwald 
678552d92a1Smatthias.ringwald     // process
679552d92a1Smatthias.ringwald     l2cap_run();
680e405ae81Smatthias.ringwald }
681645658c9Smatthias.ringwald 
682b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
683b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
684e405ae81Smatthias.ringwald     if (!channel) {
6857d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
686e405ae81Smatthias.ringwald         return;
687e405ae81Smatthias.ringwald     }
688e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
689e7ff783cSmatthias.ringwald     channel->reason = reason;
690e7ff783cSmatthias.ringwald     l2cap_run();
691645658c9Smatthias.ringwald }
692645658c9Smatthias.ringwald 
6932784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
694b1988dceSmatthias.ringwald 
695b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
696b1988dceSmatthias.ringwald 
6972784b77dSmatthias.ringwald     // accept the other's configuration options
6983de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
6993de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7003de7c0caSmatthias.ringwald     while (pos < end_pos){
7011dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7021dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7031dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7041dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7051dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
7067b5fbe1fSmatthias.ringwald             // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7071dc511deSmatthias.ringwald         }
7081dc511deSmatthias.ringwald         pos += length;
7091dc511deSmatthias.ringwald     }
7102784b77dSmatthias.ringwald }
7112784b77dSmatthias.ringwald 
712fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
7137b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
71473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
71573cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
716fa8473a4Smatthias.ringwald     return 1;
717fa8473a4Smatthias.ringwald }
718fa8473a4Smatthias.ringwald 
719fa8473a4Smatthias.ringwald 
72000d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7211e6aba47Smatthias.ringwald 
72200d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
72300d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
72438e5900eSmatthias.ringwald     uint16_t result = 0;
7251e6aba47Smatthias.ringwald 
7267b5fbe1fSmatthias.ringwald     log_info("signaling handler code %u, state %u\n", code, channel->state);
727b35f641cSmatthias.ringwald 
7289a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7299a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7309a011532Smatthias.ringwald         switch (channel->state){
731fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7329a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7332b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7349a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7359a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7369a011532Smatthias.ringwald                 break;
7379a011532Smatthias.ringwald 
7389a011532Smatthias.ringwald             default:
7399a011532Smatthias.ringwald                 // ignore in other states
7409a011532Smatthias.ringwald                 break;
7419a011532Smatthias.ringwald         }
7429a011532Smatthias.ringwald         return;
7439a011532Smatthias.ringwald     }
7449a011532Smatthias.ringwald 
7451e6aba47Smatthias.ringwald     switch (channel->state) {
7461e6aba47Smatthias.ringwald 
7471e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7481e6aba47Smatthias.ringwald             switch (code){
7491e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
75000d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
75138e5900eSmatthias.ringwald                     switch (result) {
75238e5900eSmatthias.ringwald                         case 0:
753169f8b28Smatthias.ringwald                             // successful connection
75400d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
755fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
75673cf2b3dSmatthias.ringwald                             channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
75738e5900eSmatthias.ringwald                             break;
75838e5900eSmatthias.ringwald                         case 1:
75938e5900eSmatthias.ringwald                             // connection pending. get some coffee
76038e5900eSmatthias.ringwald                             break;
76138e5900eSmatthias.ringwald                         default:
762eb920dbeSmatthias.ringwald                             // channel closed
763eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
764eb920dbeSmatthias.ringwald 
765f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
76638e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
767eb920dbeSmatthias.ringwald 
768eb920dbeSmatthias.ringwald                             // drop link key if security block
769eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
770eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
771eb920dbeSmatthias.ringwald                             }
772eb920dbeSmatthias.ringwald 
773eb920dbeSmatthias.ringwald                             // discard channel
774eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
775d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
77638e5900eSmatthias.ringwald                             break;
7771e6aba47Smatthias.ringwald                     }
7781e6aba47Smatthias.ringwald                     break;
77938e5900eSmatthias.ringwald 
78038e5900eSmatthias.ringwald                 default:
7811e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
78238e5900eSmatthias.ringwald                     break;
7831e6aba47Smatthias.ringwald             }
7841e6aba47Smatthias.ringwald             break;
7851e6aba47Smatthias.ringwald 
786fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
787ae280e73Smatthias.ringwald             switch (code) {
788ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
78973cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ;
79073cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
791ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
792ae280e73Smatthias.ringwald                     break;
7931e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
79473cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP;
7955a67bd4aSmatthias.ringwald                     break;
7965a67bd4aSmatthias.ringwald                 default:
7975a67bd4aSmatthias.ringwald                     break;
7981e6aba47Smatthias.ringwald             }
799fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
800fa8473a4Smatthias.ringwald                 // for open:
8015a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
802fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8036218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
804c8e4258aSmatthias.ringwald             }
805c8e4258aSmatthias.ringwald             break;
806f62db1e3Smatthias.ringwald 
807f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
808f62db1e3Smatthias.ringwald             switch (code) {
809f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
81027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
81127a923d0Smatthias.ringwald                     break;
8125a67bd4aSmatthias.ringwald                 default:
8135a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8145a67bd4aSmatthias.ringwald                     break;
81527a923d0Smatthias.ringwald             }
81627a923d0Smatthias.ringwald             break;
81784836b65Smatthias.ringwald 
81884836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
81984836b65Smatthias.ringwald             // @TODO handle incoming requests
82084836b65Smatthias.ringwald             break;
82184836b65Smatthias.ringwald 
82284836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
82384836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
82484836b65Smatthias.ringwald             break;
82510642e45Smatthias.ringwald         default:
82610642e45Smatthias.ringwald             break;
82727a923d0Smatthias.ringwald     }
8287b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
82927a923d0Smatthias.ringwald }
83027a923d0Smatthias.ringwald 
83100d93d79Smatthias.ringwald 
83200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
83300d93d79Smatthias.ringwald 
83400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
83500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
83600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
83700d93d79Smatthias.ringwald 
83800d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
83900d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
84000d93d79Smatthias.ringwald         return;
84100d93d79Smatthias.ringwald     }
84200d93d79Smatthias.ringwald 
84300d93d79Smatthias.ringwald     // general commands without an assigned channel
84400d93d79Smatthias.ringwald     switch(code) {
84500d93d79Smatthias.ringwald 
84600d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
84700d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
84800d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
84900d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8502b83fb7dSmatthias.ringwald             return;
85100d93d79Smatthias.ringwald         }
85200d93d79Smatthias.ringwald 
8532b360848Smatthias.ringwald         case ECHO_REQUEST:
8542b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
8552b83fb7dSmatthias.ringwald             return;
85600d93d79Smatthias.ringwald 
85700d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
85800d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
8592b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
8602b83fb7dSmatthias.ringwald             return;
86100d93d79Smatthias.ringwald         }
86200d93d79Smatthias.ringwald 
86300d93d79Smatthias.ringwald         default:
86400d93d79Smatthias.ringwald             break;
86500d93d79Smatthias.ringwald     }
86600d93d79Smatthias.ringwald 
86700d93d79Smatthias.ringwald 
86800d93d79Smatthias.ringwald     // Get potential destination CID
86900d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
87000d93d79Smatthias.ringwald 
87100d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
87200d93d79Smatthias.ringwald     linked_item_t *it;
87300d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
87400d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
87500d93d79Smatthias.ringwald         if (channel->handle == handle) {
87600d93d79Smatthias.ringwald             if (code & 1) {
877b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
878b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
87900d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8804e32727eSmatthias.ringwald                     break;
88100d93d79Smatthias.ringwald                 }
88200d93d79Smatthias.ringwald             } else {
883b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
88400d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
88500d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8864e32727eSmatthias.ringwald                     break;
88700d93d79Smatthias.ringwald                 }
88800d93d79Smatthias.ringwald             }
88900d93d79Smatthias.ringwald         }
89000d93d79Smatthias.ringwald     }
89100d93d79Smatthias.ringwald }
89200d93d79Smatthias.ringwald 
89300d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
89400d93d79Smatthias.ringwald 
89500d93d79Smatthias.ringwald     // Get Channel ID
89600d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
89700d93d79Smatthias.ringwald 
89800d93d79Smatthias.ringwald     // Signaling Packet?
89900d93d79Smatthias.ringwald     if (channel_id == 1) {
90000d93d79Smatthias.ringwald 
90100d93d79Smatthias.ringwald         // Get Connection
90200d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
90300d93d79Smatthias.ringwald 
90400d93d79Smatthias.ringwald         uint16_t command_offset = 8;
90500d93d79Smatthias.ringwald         while (command_offset < size) {
90600d93d79Smatthias.ringwald 
90700d93d79Smatthias.ringwald             // handle signaling commands
90800d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
90900d93d79Smatthias.ringwald 
91000d93d79Smatthias.ringwald             // increment command_offset
91100d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
91200d93d79Smatthias.ringwald         }
91364472d52Smatthias.ringwald 
91464472d52Smatthias.ringwald         l2cap_run();
91564472d52Smatthias.ringwald 
91600d93d79Smatthias.ringwald         return;
91700d93d79Smatthias.ringwald     }
91800d93d79Smatthias.ringwald 
91900d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
92000d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
92100d93d79Smatthias.ringwald     if (channel) {
92258de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
92300d93d79Smatthias.ringwald     }
92400d93d79Smatthias.ringwald }
92500d93d79Smatthias.ringwald 
9262718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9272718e2e7Smatthias.ringwald     switch (packet_type) {
9282718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9292718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9302718e2e7Smatthias.ringwald             break;
9312718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9322718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9332718e2e7Smatthias.ringwald             break;
9342718e2e7Smatthias.ringwald         default:
9352718e2e7Smatthias.ringwald             break;
9362718e2e7Smatthias.ringwald     }
9372718e2e7Smatthias.ringwald }
93800d93d79Smatthias.ringwald 
93915ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
94027a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
941f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
942f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
943f62db1e3Smatthias.ringwald     // discard channel
944f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
945d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
946c8e4258aSmatthias.ringwald }
9471e6aba47Smatthias.ringwald 
9489d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
949c52bf64dSmatthias.ringwald     linked_item_t *it;
9509d9bbc01Smatthias.ringwald 
9519d9bbc01Smatthias.ringwald     // close open channels
9529d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9539d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9549d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9559d9bbc01Smatthias.ringwald             return service;
9569d9bbc01Smatthias.ringwald         };
9579d9bbc01Smatthias.ringwald     }
9589d9bbc01Smatthias.ringwald     return NULL;
9599d9bbc01Smatthias.ringwald }
9609d9bbc01Smatthias.ringwald 
96136944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
9624bb582b6Smatthias.ringwald     // check for alread registered psm
9634bb582b6Smatthias.ringwald     // TODO: emit error event
9649d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
965277abc2cSmatthias.ringwald     if (service) {
966277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
967277abc2cSmatthias.ringwald         return;
968277abc2cSmatthias.ringwald     }
9699d9bbc01Smatthias.ringwald 
9704bb582b6Smatthias.ringwald     // alloc structure
9714bb582b6Smatthias.ringwald     // TODO: emit error event
972d3a9df87Smatthias.ringwald     service = btstack_memory_l2cap_service_get();
973277abc2cSmatthias.ringwald     if (!service) {
974277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
975277abc2cSmatthias.ringwald         return;
976277abc2cSmatthias.ringwald     }
9779d9bbc01Smatthias.ringwald 
9789d9bbc01Smatthias.ringwald     // fill in
9799d9bbc01Smatthias.ringwald     service->psm = psm;
9809d9bbc01Smatthias.ringwald     service->mtu = mtu;
9819d9bbc01Smatthias.ringwald     service->connection = connection;
982d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
9839d9bbc01Smatthias.ringwald 
9849d9bbc01Smatthias.ringwald     // add to services list
9859d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
9869d9bbc01Smatthias.ringwald }
9879d9bbc01Smatthias.ringwald 
98836944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
9899d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
990037d6e48Smatthias.ringwald     if (!service) return;
9919d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
992d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
9939d9bbc01Smatthias.ringwald }
9949d9bbc01Smatthias.ringwald 
9959d9bbc01Smatthias.ringwald //
99636944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
9979d9bbc01Smatthias.ringwald     linked_item_t *it;
9989d9bbc01Smatthias.ringwald 
9993a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1000c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1001c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1002c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1003c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1004cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1005c52bf64dSmatthias.ringwald         }
1006c52bf64dSmatthias.ringwald     }
10079d9bbc01Smatthias.ringwald 
1008645658c9Smatthias.ringwald     // unregister services
100969025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
101069025de8Smatthias.ringwald     while (it->next) {
101169025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1012645658c9Smatthias.ringwald         if (service->connection == connection){
101369025de8Smatthias.ringwald             it->next = it->next->next;
1014d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
10158149d2c7Smatthias.ringwald         } else {
10168149d2c7Smatthias.ringwald             it = it->next;
1017645658c9Smatthias.ringwald         }
1018645658c9Smatthias.ringwald     }
1019cb8eb7e6Smatthias.ringwald 
1020cb8eb7e6Smatthias.ringwald     // process
1021cb8eb7e6Smatthias.ringwald     l2cap_run();
1022c52bf64dSmatthias.ringwald }
1023c52bf64dSmatthias.ringwald