xref: /btstack/src/l2cap.c (revision 56081214e8a275b8daecd57cbbf96c0ec102a979)
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 
514c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
524c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
534c744e21Smatthias.ringwald 
5439bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
55e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
5639bda6d5Smatthias.ringwald 
5700d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
5800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
5900d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6000d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6100d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6200d93d79Smatthias.ringwald 
6339bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
6439bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
6539bda6d5Smatthias.ringwald 
6639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
672b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
682b83fb7dSmatthias.ringwald static int signaling_responses_pending;
692b83fb7dSmatthias.ringwald 
701e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
719d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
7236944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
73808a48abSmatthias.ringwald static int new_credits_blocked = 0;
741e6aba47Smatthias.ringwald 
7539bda6d5Smatthias.ringwald // prototypes
76fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
77fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
78fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
79fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
80fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
81fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8239bda6d5Smatthias.ringwald 
8339bda6d5Smatthias.ringwald 
841e6aba47Smatthias.ringwald void l2cap_init(){
85fcadd0caSmatthias.ringwald 
86808a48abSmatthias.ringwald     new_credits_blocked = 0;
872b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
88808a48abSmatthias.ringwald 
89fcadd0caSmatthias.ringwald     //
902718e2e7Smatthias.ringwald     // register callback with HCI
91fcadd0caSmatthias.ringwald     //
922718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
93fcadd0caSmatthias.ringwald }
94fcadd0caSmatthias.ringwald 
95fcadd0caSmatthias.ringwald 
96fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
9736944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
98fcadd0caSmatthias.ringwald }
9936944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
100b502e1b0Smatthias.ringwald     packet_handler = handler;
1011e6aba47Smatthias.ringwald }
1021e6aba47Smatthias.ringwald 
10358de5610Smatthias.ringwald //  notify client/protocol handler
10458de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
10558de5610Smatthias.ringwald     if (channel->packet_handler) {
10658de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
10758de5610Smatthias.ringwald     } else {
10836944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
10958de5610Smatthias.ringwald     }
11058de5610Smatthias.ringwald }
11158de5610Smatthias.ringwald 
11258de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1139893b714Smatthias.ringwald     uint8_t event[21];
11458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
11558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
11658de5610Smatthias.ringwald     event[2] = status;
11758de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
11858de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
11958de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
12058de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
12158de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1224c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1234c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
12458de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
12558de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
12658de5610Smatthias.ringwald }
12758de5610Smatthias.ringwald 
12858de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
12958de5610Smatthias.ringwald     uint8_t event[4];
13058de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
13158de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13258de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
13358de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13458de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13558de5610Smatthias.ringwald }
13658de5610Smatthias.ringwald 
13758de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
13858de5610Smatthias.ringwald     uint8_t event[16];
13958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
14058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14158de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
14258de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
14358de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
14458de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
14558de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
14658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
14758de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1480af41d30Smatthias.ringwald }
149808a48abSmatthias.ringwald 
1506218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1516218e6f1Smatthias.ringwald     // track credits
1526218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1537b5fbe1fSmatthias.ringwald     // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1546218e6f1Smatthias.ringwald 
1556218e6f1Smatthias.ringwald     uint8_t event[5];
1566218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1576218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1586218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1596218e6f1Smatthias.ringwald     event[4] = credits;
1606218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1616218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1626218e6f1Smatthias.ringwald }
1636218e6f1Smatthias.ringwald 
164808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
165808a48abSmatthias.ringwald     new_credits_blocked = blocked;
166808a48abSmatthias.ringwald }
167808a48abSmatthias.ringwald 
16840d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
169808a48abSmatthias.ringwald 
170808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
171808a48abSmatthias.ringwald 
1728d371091Smatthias.ringwald     linked_item_t *it;
1738d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1748d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1758d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1760e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1774c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1788d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1798d371091Smatthias.ringwald         }
1808d371091Smatthias.ringwald     }
1818d371091Smatthias.ringwald }
1828d371091Smatthias.ringwald 
183b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
184f62db1e3Smatthias.ringwald     linked_item_t *it;
185f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1868d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
187b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
188f62db1e3Smatthias.ringwald             return channel;
189f62db1e3Smatthias.ringwald         }
190f62db1e3Smatthias.ringwald     }
191f62db1e3Smatthias.ringwald     return NULL;
192f62db1e3Smatthias.ringwald }
193f62db1e3Smatthias.ringwald 
1946b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
1956b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
1966b1fde37Smatthias.ringwald     if (!channel) return 0;
1976b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
1986b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
1996b1fde37Smatthias.ringwald }
2006b1fde37Smatthias.ringwald 
20196cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
20296cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
20396cbd662Smatthias.ringwald     if (channel) {
20496cbd662Smatthias.ringwald         return channel->remote_mtu;
20596cbd662Smatthias.ringwald     }
20696cbd662Smatthias.ringwald     return 0;
20796cbd662Smatthias.ringwald }
20896cbd662Smatthias.ringwald 
20958de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
210b1d43497Smatthias.ringwald 
211b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
212b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
213b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
214b1d43497Smatthias.ringwald     }
215b1d43497Smatthias.ringwald 
2167b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
217b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
21858de5610Smatthias.ringwald     va_list argptr;
21958de5610Smatthias.ringwald     va_start(argptr, identifier);
220816c0598Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
22158de5610Smatthias.ringwald     va_end(argptr);
2227b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
223816c0598Smatthias.ringwald     return hci_send_acl_packet(acl_buffer, len);
22458de5610Smatthias.ringwald }
22558de5610Smatthias.ringwald 
226b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
227b1d43497Smatthias.ringwald     return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
228b1d43497Smatthias.ringwald }
2296218e6f1Smatthias.ringwald 
230b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
231b1d43497Smatthias.ringwald 
232b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
233b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
234808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2358ea03fa5Smatthias.ringwald     }
2366218e6f1Smatthias.ringwald 
23758de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
238b1d43497Smatthias.ringwald     if (!channel) {
239b1d43497Smatthias.ringwald         log_error("l2cap_send_internal no channel for cid %u\n", local_cid);
240b1d43497Smatthias.ringwald         return -1;   // TODO: define error
2416218e6f1Smatthias.ringwald     }
2426218e6f1Smatthias.ringwald 
243b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
244b1d43497Smatthias.ringwald         log_error("l2cap_send_internal cid %u, no credits!\n", local_cid);
245b1d43497Smatthias.ringwald         return -1;  // TODO: define error
246b1d43497Smatthias.ringwald     }
247b1d43497Smatthias.ringwald 
248b1d43497Smatthias.ringwald     --channel->packets_granted;
249b1d43497Smatthias.ringwald 
250b1d43497Smatthias.ringwald     log_debug("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
251b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
252b1d43497Smatthias.ringwald 
253b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
254b1d43497Smatthias.ringwald 
25558de5610Smatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
25658de5610Smatthias.ringwald     bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
25758de5610Smatthias.ringwald     // 2 - ACL length
25858de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
25958de5610Smatthias.ringwald     // 4 - L2CAP packet length
26058de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
26158de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
26258de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
26358de5610Smatthias.ringwald     // send
264b1d43497Smatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
26591b99603Smatthias.ringwald 
26691b99603Smatthias.ringwald     l2cap_hand_out_credits();
26791b99603Smatthias.ringwald 
2686218e6f1Smatthias.ringwald     return err;
26958de5610Smatthias.ringwald }
27058de5610Smatthias.ringwald 
271b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
272b1d43497Smatthias.ringwald 
273b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
274b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
275b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
276b1d43497Smatthias.ringwald     }
277b1d43497Smatthias.ringwald 
278b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
279b1d43497Smatthias.ringwald 
280b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
281b1d43497Smatthias.ringwald 
282b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
283b1d43497Smatthias.ringwald }
284b1d43497Smatthias.ringwald 
285b1d43497Smatthias.ringwald 
2868158c421Smatthias.ringwald // MARK: L2CAP_RUN
2872cd0be45Smatthias.ringwald // process outstanding signaling tasks
2882cd0be45Smatthias.ringwald void l2cap_run(void){
2892b83fb7dSmatthias.ringwald 
2902b83fb7dSmatthias.ringwald     // check pending signaling responses
2912b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
2922b83fb7dSmatthias.ringwald 
29302b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
2942b83fb7dSmatthias.ringwald 
2952b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
2962b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
2972b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
2982b360848Smatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST
2992b83fb7dSmatthias.ringwald 
3002b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3012b360848Smatthias.ringwald             case CONNECTION_REQUEST:
3022b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
3032b360848Smatthias.ringwald                 break;
3042b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3052b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3062b83fb7dSmatthias.ringwald                 break;
3072b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3082b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3092b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3102b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3112b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3122b83fb7dSmatthias.ringwald                 } else {
3132b83fb7dSmatthias.ringwald                     // all other types are not supported
3142b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3152b83fb7dSmatthias.ringwald                 }
3162b83fb7dSmatthias.ringwald                 break;
3172b83fb7dSmatthias.ringwald             default:
3182b83fb7dSmatthias.ringwald                 // should not happen
3192b83fb7dSmatthias.ringwald                 break;
3202b83fb7dSmatthias.ringwald         }
3212b83fb7dSmatthias.ringwald 
3222b83fb7dSmatthias.ringwald         // remove first item
3232b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3242b83fb7dSmatthias.ringwald         int i;
3252b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
32676115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
3272b83fb7dSmatthias.ringwald         }
3282b83fb7dSmatthias.ringwald     }
3292b83fb7dSmatthias.ringwald 
330ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3312cd0be45Smatthias.ringwald     linked_item_t *it;
3322cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3332cd0be45Smatthias.ringwald 
33402b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
33502b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3362cd0be45Smatthias.ringwald 
3372cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
3386e6710ebSmatthias.ringwald 
3397b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
3406e6710ebSmatthias.ringwald 
341*56081214Smatthias.ringwald 
3422cd0be45Smatthias.ringwald         switch (channel->state){
3432cd0be45Smatthias.ringwald 
34402b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
34564472d52Smatthias.ringwald                 // send connection request - set state first
34664472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
34702b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
3488f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
34902b22dc4Smatthias.ringwald                 break;
35002b22dc4Smatthias.ringwald 
351e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
352b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
353e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
354e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
355d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
356e7ff783cSmatthias.ringwald                 break;
357e7ff783cSmatthias.ringwald 
358552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
359fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
36073cf2b3dSmatthias.ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
3612a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
362552d92a1Smatthias.ringwald                 break;
363552d92a1Smatthias.ringwald 
3646fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
3656fdcc387Smatthias.ringwald                 // success, start l2cap handshake
366b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
3676fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
3682a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
3696fdcc387Smatthias.ringwald                 break;
3706fdcc387Smatthias.ringwald 
371fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
37273cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
37373cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
37473cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP;
375fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
376fa8473a4Smatthias.ringwald                 }
37773cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
37873cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
37973cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ;
380b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
381ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
382ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
383ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
384b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
385fa8473a4Smatthias.ringwald                 }
386fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
387552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
388552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
389552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
390fa8473a4Smatthias.ringwald                 }
391552d92a1Smatthias.ringwald                 break;
392552d92a1Smatthias.ringwald 
393e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
394b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
395e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
396e7ff783cSmatthias.ringwald                 break;
397e7ff783cSmatthias.ringwald 
398e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
399b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4002cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4012a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4022cd0be45Smatthias.ringwald                 break;
4032cd0be45Smatthias.ringwald             default:
4042cd0be45Smatthias.ringwald                 break;
4052cd0be45Smatthias.ringwald         }
4062cd0be45Smatthias.ringwald     }
4072cd0be45Smatthias.ringwald }
4082cd0be45Smatthias.ringwald 
4094aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
410816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
411fa8c92f6Smatthias.ringwald }
412fa8c92f6Smatthias.ringwald 
4131e6aba47Smatthias.ringwald // open outgoing L2CAP channel
41415470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
41515470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4161e6aba47Smatthias.ringwald 
4171e6aba47Smatthias.ringwald     // alloc structure
418d3a9df87Smatthias.ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
4192b360848Smatthias.ringwald     if (!chan) {
4202b360848Smatthias.ringwald         // emit error event
4212b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
4222b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
4232b360848Smatthias.ringwald         dummy_channel.psm = psm;
4242b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
4252b360848Smatthias.ringwald         return;
4262b360848Smatthias.ringwald     }
4271d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
4282985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
4292985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
4309775e25bSmatthias.ringwald     }
4319775e25bSmatthias.ringwald 
4321e6aba47Smatthias.ringwald     // fill in
4331e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4341e6aba47Smatthias.ringwald     chan->psm = psm;
4351e6aba47Smatthias.ringwald     chan->handle = 0;
4361e6aba47Smatthias.ringwald     chan->connection = connection;
4376b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4382784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
43915470d27Smatthias.ringwald     chan->local_mtu = mtu;
4406218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4416218e6f1Smatthias.ringwald 
4421e6aba47Smatthias.ringwald     // set initial state
44302b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
44473cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
445b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
446b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
4471e6aba47Smatthias.ringwald 
4481e6aba47Smatthias.ringwald     // add to connections list
4491e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
4501e6aba47Smatthias.ringwald 
45102b22dc4Smatthias.ringwald     l2cap_run();
45243625864Smatthias.ringwald }
45343625864Smatthias.ringwald 
454b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
455b35f641cSmatthias.ringwald     // find channel for local_cid
456b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
457f62db1e3Smatthias.ringwald     if (channel) {
458e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
459f62db1e3Smatthias.ringwald     }
4602cd0be45Smatthias.ringwald     // process
4612cd0be45Smatthias.ringwald     l2cap_run();
46243625864Smatthias.ringwald }
4631e6aba47Smatthias.ringwald 
464afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
46515ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
46615ec09bbSmatthias.ringwald     while (it->next){
46715ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
468b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
46902b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
470afde0c52Smatthias.ringwald                 // failure, forward error code
471afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
472afde0c52Smatthias.ringwald                 // discard channel
47315ec09bbSmatthias.ringwald                 it->next = it->next->next;
474d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
475afde0c52Smatthias.ringwald             }
47615ec09bbSmatthias.ringwald         } else {
47715ec09bbSmatthias.ringwald             it = it->next;
478afde0c52Smatthias.ringwald         }
479afde0c52Smatthias.ringwald     }
480afde0c52Smatthias.ringwald }
481afde0c52Smatthias.ringwald 
482afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
483afde0c52Smatthias.ringwald     linked_item_t *it;
484afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
485afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
486afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
48702b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
488b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
4896fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
490afde0c52Smatthias.ringwald                 channel->handle = handle;
491b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
492afde0c52Smatthias.ringwald             }
493afde0c52Smatthias.ringwald         }
494afde0c52Smatthias.ringwald     }
4956fdcc387Smatthias.ringwald     // process
4966fdcc387Smatthias.ringwald     l2cap_run();
497afde0c52Smatthias.ringwald }
498b448a0e7Smatthias.ringwald 
499afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
500afde0c52Smatthias.ringwald 
501afde0c52Smatthias.ringwald     bd_addr_t address;
502afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5036e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
50436944dffSmatthias.ringwald     linked_item_t *it;
5052d00edd4Smatthias.ringwald     int hci_con_used;
506afde0c52Smatthias.ringwald 
507afde0c52Smatthias.ringwald     switch(packet[0]){
508afde0c52Smatthias.ringwald 
509afde0c52Smatthias.ringwald         // handle connection complete events
510afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
511afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
512afde0c52Smatthias.ringwald             if (packet[2] == 0){
513afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
514afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
515afde0c52Smatthias.ringwald             } else {
516afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
517afde0c52Smatthias.ringwald             }
518afde0c52Smatthias.ringwald             break;
519afde0c52Smatthias.ringwald 
520afde0c52Smatthias.ringwald         // handle successful create connection cancel command
521afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
522afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
523afde0c52Smatthias.ringwald                 if (packet[5] == 0){
524afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
525afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
526afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
52703cfbabcSmatthias.ringwald                 }
5281e6aba47Smatthias.ringwald             }
52939d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
53039d59809Smatthias.ringwald             break;
53139d59809Smatthias.ringwald 
53239d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
53339d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
534afde0c52Smatthias.ringwald             break;
53527a923d0Smatthias.ringwald 
5361e6aba47Smatthias.ringwald         // handle disconnection complete events
537afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
53827a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
539afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
54015ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
54115ec09bbSmatthias.ringwald             while (it->next){
54239ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
54327a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
54415ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
5452060cf14Smatthias.ringwald                     it->next = it->next->next;
54615ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
547d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
54815ec09bbSmatthias.ringwald                 } else {
54915ec09bbSmatthias.ringwald                     it = it->next;
55027a923d0Smatthias.ringwald                 }
55127a923d0Smatthias.ringwald             }
552afde0c52Smatthias.ringwald             break;
553fcadd0caSmatthias.ringwald 
5546218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
55502b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
5568d371091Smatthias.ringwald             l2cap_hand_out_credits();
5576218e6f1Smatthias.ringwald             break;
5586218e6f1Smatthias.ringwald 
559ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
560afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
56136944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
56280ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
5632d00edd4Smatthias.ringwald             hci_con_used = 0;
564ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
565ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
566ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
5672d00edd4Smatthias.ringwald                     hci_con_used = 1;
568ee091cf1Smatthias.ringwald                 }
569ee091cf1Smatthias.ringwald             }
5702d00edd4Smatthias.ringwald             if (hci_con_used) break;
57132ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
5729edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
573afde0c52Smatthias.ringwald             break;
574ee091cf1Smatthias.ringwald 
5756e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
5766e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
5776e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
5786e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
5796e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
5806e6710ebSmatthias.ringwald                 }
5816e6710ebSmatthias.ringwald             }
5826e6710ebSmatthias.ringwald             break;
5836e6710ebSmatthias.ringwald 
584afde0c52Smatthias.ringwald         default:
585afde0c52Smatthias.ringwald             break;
586afde0c52Smatthias.ringwald     }
587afde0c52Smatthias.ringwald 
588afde0c52Smatthias.ringwald     // pass on
589b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
5901e6aba47Smatthias.ringwald }
5911e6aba47Smatthias.ringwald 
592afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
593b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
594e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
595e7ff783cSmatthias.ringwald     l2cap_run();
59684836b65Smatthias.ringwald }
59784836b65Smatthias.ringwald 
5982b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
5992b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
6002b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
6012b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
6022b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
6032b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
6042b360848Smatthias.ringwald         signaling_responses_pending++;
6052b360848Smatthias.ringwald         l2cap_run();
6062b360848Smatthias.ringwald     }
6072b360848Smatthias.ringwald }
6082b360848Smatthias.ringwald 
609b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
610645658c9Smatthias.ringwald 
6117b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
612645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
613645658c9Smatthias.ringwald     if (!service) {
614645658c9Smatthias.ringwald         // 0x0002 PSM not supported
6152b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
616645658c9Smatthias.ringwald         return;
617645658c9Smatthias.ringwald     }
618645658c9Smatthias.ringwald 
619645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
620645658c9Smatthias.ringwald     if (!hci_connection) {
6212b360848Smatthias.ringwald         //
6227d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
623645658c9Smatthias.ringwald         return;
624645658c9Smatthias.ringwald     }
625645658c9Smatthias.ringwald     // alloc structure
6267b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
627d3a9df87Smatthias.ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
6282b360848Smatthias.ringwald     if (!channel){
6292b360848Smatthias.ringwald         // 0x0004 No resources available
6302b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
6312b360848Smatthias.ringwald         return;
6322b360848Smatthias.ringwald     }
633645658c9Smatthias.ringwald 
634645658c9Smatthias.ringwald     // fill in
635169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
636169f8b28Smatthias.ringwald     channel->psm = psm;
637169f8b28Smatthias.ringwald     channel->handle = handle;
638169f8b28Smatthias.ringwald     channel->connection = service->connection;
639f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
640b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
641b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
642fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6430a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
644761b0451Smatthias.ringwald     channel->packets_granted = 0;
645b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
646645658c9Smatthias.ringwald 
6471d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
6482985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
6492985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
6509775e25bSmatthias.ringwald     }
6519775e25bSmatthias.ringwald 
652645658c9Smatthias.ringwald     // set initial state
653e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
65473cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
655e405ae81Smatthias.ringwald 
656645658c9Smatthias.ringwald     // add to connections list
657169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
658645658c9Smatthias.ringwald 
659e405ae81Smatthias.ringwald     // emit incoming connection request
660e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
661e405ae81Smatthias.ringwald }
662645658c9Smatthias.ringwald 
663b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
664b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
665e405ae81Smatthias.ringwald     if (!channel) {
6667d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
667e405ae81Smatthias.ringwald         return;
668e405ae81Smatthias.ringwald     }
669e405ae81Smatthias.ringwald 
670552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
671e405ae81Smatthias.ringwald 
672552d92a1Smatthias.ringwald     // process
673552d92a1Smatthias.ringwald     l2cap_run();
674e405ae81Smatthias.ringwald }
675645658c9Smatthias.ringwald 
676b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
677b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
678e405ae81Smatthias.ringwald     if (!channel) {
6797d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
680e405ae81Smatthias.ringwald         return;
681e405ae81Smatthias.ringwald     }
682e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
683e7ff783cSmatthias.ringwald     channel->reason = reason;
684e7ff783cSmatthias.ringwald     l2cap_run();
685645658c9Smatthias.ringwald }
686645658c9Smatthias.ringwald 
6872784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
688b1988dceSmatthias.ringwald 
689b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
690b1988dceSmatthias.ringwald 
6912784b77dSmatthias.ringwald     // accept the other's configuration options
6923de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
6933de7c0caSmatthias.ringwald     uint16_t pos     = 8;
6943de7c0caSmatthias.ringwald     while (pos < end_pos){
6951dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
6961dc511deSmatthias.ringwald         uint8_t length = command[pos++];
6971dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
6981dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
6991dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
7007b5fbe1fSmatthias.ringwald             // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7011dc511deSmatthias.ringwald         }
7021dc511deSmatthias.ringwald         pos += length;
7031dc511deSmatthias.ringwald     }
7042784b77dSmatthias.ringwald }
7052784b77dSmatthias.ringwald 
706fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
7077b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
70873cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
70973cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
710fa8473a4Smatthias.ringwald     return 1;
711fa8473a4Smatthias.ringwald }
712fa8473a4Smatthias.ringwald 
713fa8473a4Smatthias.ringwald 
71400d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7151e6aba47Smatthias.ringwald 
71600d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
71700d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
71838e5900eSmatthias.ringwald     uint16_t result = 0;
7191e6aba47Smatthias.ringwald 
7207b5fbe1fSmatthias.ringwald     log_info("signaling handler code %u, state %u\n", code, channel->state);
721b35f641cSmatthias.ringwald 
7229a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7239a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7249a011532Smatthias.ringwald         switch (channel->state){
725fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7269a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7272b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7289a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7299a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7309a011532Smatthias.ringwald                 break;
7319a011532Smatthias.ringwald 
7329a011532Smatthias.ringwald             default:
7339a011532Smatthias.ringwald                 // ignore in other states
7349a011532Smatthias.ringwald                 break;
7359a011532Smatthias.ringwald         }
7369a011532Smatthias.ringwald         return;
7379a011532Smatthias.ringwald     }
7389a011532Smatthias.ringwald 
739*56081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
7401e6aba47Smatthias.ringwald     switch (channel->state) {
7411e6aba47Smatthias.ringwald 
7421e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7431e6aba47Smatthias.ringwald             switch (code){
7441e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
74500d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
74638e5900eSmatthias.ringwald                     switch (result) {
74738e5900eSmatthias.ringwald                         case 0:
748169f8b28Smatthias.ringwald                             // successful connection
74900d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
750fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
75173cf2b3dSmatthias.ringwald                             channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
75238e5900eSmatthias.ringwald                             break;
75338e5900eSmatthias.ringwald                         case 1:
75438e5900eSmatthias.ringwald                             // connection pending. get some coffee
75538e5900eSmatthias.ringwald                             break;
75638e5900eSmatthias.ringwald                         default:
757eb920dbeSmatthias.ringwald                             // channel closed
758eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
759eb920dbeSmatthias.ringwald 
760f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
76138e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
762eb920dbeSmatthias.ringwald 
763eb920dbeSmatthias.ringwald                             // drop link key if security block
764eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
765eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
766eb920dbeSmatthias.ringwald                             }
767eb920dbeSmatthias.ringwald 
768eb920dbeSmatthias.ringwald                             // discard channel
769eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
770d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
77138e5900eSmatthias.ringwald                             break;
7721e6aba47Smatthias.ringwald                     }
7731e6aba47Smatthias.ringwald                     break;
77438e5900eSmatthias.ringwald 
77538e5900eSmatthias.ringwald                 default:
7761e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
77738e5900eSmatthias.ringwald                     break;
7781e6aba47Smatthias.ringwald             }
7791e6aba47Smatthias.ringwald             break;
7801e6aba47Smatthias.ringwald 
781fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
782ae280e73Smatthias.ringwald             switch (code) {
783ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
78473cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ;
78573cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
786ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
787ae280e73Smatthias.ringwald                     break;
7881e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
78973cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP;
7905a67bd4aSmatthias.ringwald                     break;
7915a67bd4aSmatthias.ringwald                 default:
7925a67bd4aSmatthias.ringwald                     break;
7931e6aba47Smatthias.ringwald             }
794fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
795fa8473a4Smatthias.ringwald                 // for open:
7965a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
797fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
7986218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
799c8e4258aSmatthias.ringwald             }
800c8e4258aSmatthias.ringwald             break;
801f62db1e3Smatthias.ringwald 
802f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
803f62db1e3Smatthias.ringwald             switch (code) {
804f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
80527a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
80627a923d0Smatthias.ringwald                     break;
8075a67bd4aSmatthias.ringwald                 default:
8085a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8095a67bd4aSmatthias.ringwald                     break;
81027a923d0Smatthias.ringwald             }
81127a923d0Smatthias.ringwald             break;
81284836b65Smatthias.ringwald 
81384836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
81484836b65Smatthias.ringwald             // @TODO handle incoming requests
81584836b65Smatthias.ringwald             break;
81684836b65Smatthias.ringwald 
81784836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
81884836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
81984836b65Smatthias.ringwald             break;
82010642e45Smatthias.ringwald         default:
82110642e45Smatthias.ringwald             break;
82227a923d0Smatthias.ringwald     }
8237b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
82427a923d0Smatthias.ringwald }
82527a923d0Smatthias.ringwald 
82600d93d79Smatthias.ringwald 
82700d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
82800d93d79Smatthias.ringwald 
82900d93d79Smatthias.ringwald     // get code, signalind identifier and command len
83000d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
83100d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
83200d93d79Smatthias.ringwald 
83300d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
83400d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
83500d93d79Smatthias.ringwald         return;
83600d93d79Smatthias.ringwald     }
83700d93d79Smatthias.ringwald 
83800d93d79Smatthias.ringwald     // general commands without an assigned channel
83900d93d79Smatthias.ringwald     switch(code) {
84000d93d79Smatthias.ringwald 
84100d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
84200d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
84300d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
84400d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8452b83fb7dSmatthias.ringwald             return;
84600d93d79Smatthias.ringwald         }
84700d93d79Smatthias.ringwald 
8482b360848Smatthias.ringwald         case ECHO_REQUEST:
8492b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
8502b83fb7dSmatthias.ringwald             return;
85100d93d79Smatthias.ringwald 
85200d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
85300d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
8542b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
8552b83fb7dSmatthias.ringwald             return;
85600d93d79Smatthias.ringwald         }
85700d93d79Smatthias.ringwald 
85800d93d79Smatthias.ringwald         default:
85900d93d79Smatthias.ringwald             break;
86000d93d79Smatthias.ringwald     }
86100d93d79Smatthias.ringwald 
86200d93d79Smatthias.ringwald 
86300d93d79Smatthias.ringwald     // Get potential destination CID
86400d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
86500d93d79Smatthias.ringwald 
86600d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
86700d93d79Smatthias.ringwald     linked_item_t *it;
86800d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
86900d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
87000d93d79Smatthias.ringwald         if (channel->handle == handle) {
87100d93d79Smatthias.ringwald             if (code & 1) {
872b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
873b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
87400d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8754e32727eSmatthias.ringwald                     break;
87600d93d79Smatthias.ringwald                 }
87700d93d79Smatthias.ringwald             } else {
878b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
87900d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
88000d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8814e32727eSmatthias.ringwald                     break;
88200d93d79Smatthias.ringwald                 }
88300d93d79Smatthias.ringwald             }
88400d93d79Smatthias.ringwald         }
88500d93d79Smatthias.ringwald     }
88600d93d79Smatthias.ringwald }
88700d93d79Smatthias.ringwald 
88800d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
88900d93d79Smatthias.ringwald 
89000d93d79Smatthias.ringwald     // Get Channel ID
89100d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
89200d93d79Smatthias.ringwald 
89300d93d79Smatthias.ringwald     // Signaling Packet?
89400d93d79Smatthias.ringwald     if (channel_id == 1) {
89500d93d79Smatthias.ringwald 
89600d93d79Smatthias.ringwald         // Get Connection
89700d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
89800d93d79Smatthias.ringwald 
89900d93d79Smatthias.ringwald         uint16_t command_offset = 8;
90000d93d79Smatthias.ringwald         while (command_offset < size) {
90100d93d79Smatthias.ringwald 
90200d93d79Smatthias.ringwald             // handle signaling commands
90300d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
90400d93d79Smatthias.ringwald 
90500d93d79Smatthias.ringwald             // increment command_offset
90600d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
90700d93d79Smatthias.ringwald         }
90864472d52Smatthias.ringwald 
90964472d52Smatthias.ringwald         l2cap_run();
91064472d52Smatthias.ringwald 
91100d93d79Smatthias.ringwald         return;
91200d93d79Smatthias.ringwald     }
91300d93d79Smatthias.ringwald 
91400d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
91500d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
91600d93d79Smatthias.ringwald     if (channel) {
91758de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
91800d93d79Smatthias.ringwald     }
91900d93d79Smatthias.ringwald }
92000d93d79Smatthias.ringwald 
9212718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9222718e2e7Smatthias.ringwald     switch (packet_type) {
9232718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9242718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9252718e2e7Smatthias.ringwald             break;
9262718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9272718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9282718e2e7Smatthias.ringwald             break;
9292718e2e7Smatthias.ringwald         default:
9302718e2e7Smatthias.ringwald             break;
9312718e2e7Smatthias.ringwald     }
9322718e2e7Smatthias.ringwald }
93300d93d79Smatthias.ringwald 
93415ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
93527a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
936f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
937f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
938f62db1e3Smatthias.ringwald     // discard channel
939f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
940d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
941c8e4258aSmatthias.ringwald }
9421e6aba47Smatthias.ringwald 
9439d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
944c52bf64dSmatthias.ringwald     linked_item_t *it;
9459d9bbc01Smatthias.ringwald 
9469d9bbc01Smatthias.ringwald     // close open channels
9479d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9489d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9499d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9509d9bbc01Smatthias.ringwald             return service;
9519d9bbc01Smatthias.ringwald         };
9529d9bbc01Smatthias.ringwald     }
9539d9bbc01Smatthias.ringwald     return NULL;
9549d9bbc01Smatthias.ringwald }
9559d9bbc01Smatthias.ringwald 
95636944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
9574bb582b6Smatthias.ringwald     // check for alread registered psm
9584bb582b6Smatthias.ringwald     // TODO: emit error event
9599d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
960277abc2cSmatthias.ringwald     if (service) {
961277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
962277abc2cSmatthias.ringwald         return;
963277abc2cSmatthias.ringwald     }
9649d9bbc01Smatthias.ringwald 
9654bb582b6Smatthias.ringwald     // alloc structure
9664bb582b6Smatthias.ringwald     // TODO: emit error event
967d3a9df87Smatthias.ringwald     service = btstack_memory_l2cap_service_get();
968277abc2cSmatthias.ringwald     if (!service) {
969277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
970277abc2cSmatthias.ringwald         return;
971277abc2cSmatthias.ringwald     }
9729d9bbc01Smatthias.ringwald 
9739d9bbc01Smatthias.ringwald     // fill in
9749d9bbc01Smatthias.ringwald     service->psm = psm;
9759d9bbc01Smatthias.ringwald     service->mtu = mtu;
9769d9bbc01Smatthias.ringwald     service->connection = connection;
977d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
9789d9bbc01Smatthias.ringwald 
9799d9bbc01Smatthias.ringwald     // add to services list
9809d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
9819d9bbc01Smatthias.ringwald }
9829d9bbc01Smatthias.ringwald 
98336944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
9849d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
985037d6e48Smatthias.ringwald     if (!service) return;
9869d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
987d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
9889d9bbc01Smatthias.ringwald }
9899d9bbc01Smatthias.ringwald 
9909d9bbc01Smatthias.ringwald //
99136944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
9929d9bbc01Smatthias.ringwald     linked_item_t *it;
9939d9bbc01Smatthias.ringwald 
9943a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
995c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
996c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
997c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
998c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
999cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1000c52bf64dSmatthias.ringwald         }
1001c52bf64dSmatthias.ringwald     }
10029d9bbc01Smatthias.ringwald 
1003645658c9Smatthias.ringwald     // unregister services
100469025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
100569025de8Smatthias.ringwald     while (it->next) {
100669025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1007645658c9Smatthias.ringwald         if (service->connection == connection){
100869025de8Smatthias.ringwald             it->next = it->next->next;
1009d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
10108149d2c7Smatthias.ringwald         } else {
10118149d2c7Smatthias.ringwald             it = it->next;
1012645658c9Smatthias.ringwald         }
1013645658c9Smatthias.ringwald     }
1014cb8eb7e6Smatthias.ringwald 
1015cb8eb7e6Smatthias.ringwald     // process
1016cb8eb7e6Smatthias.ringwald     l2cap_run();
1017c52bf64dSmatthias.ringwald }
1018c52bf64dSmatthias.ringwald