xref: /btstack/src/l2cap.c (revision f5454fc67e593c7eb8a59de8fc7404f6705d5be2)
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(){
85808a48abSmatthias.ringwald     new_credits_blocked = 0;
862b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
87808a48abSmatthias.ringwald 
88*f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
89*f5454fc6Smatthias.ringwald     l2cap_services = NULL;
90*f5454fc6Smatthias.ringwald 
91*f5454fc6Smatthias.ringwald     packet_handler = null_packet_handler;
92*f5454fc6Smatthias.ringwald 
93fcadd0caSmatthias.ringwald     //
942718e2e7Smatthias.ringwald     // register callback with HCI
95fcadd0caSmatthias.ringwald     //
962718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
97fcadd0caSmatthias.ringwald }
98fcadd0caSmatthias.ringwald 
99fcadd0caSmatthias.ringwald 
100fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
10136944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
102fcadd0caSmatthias.ringwald }
10336944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
104b502e1b0Smatthias.ringwald     packet_handler = handler;
1051e6aba47Smatthias.ringwald }
1061e6aba47Smatthias.ringwald 
10758de5610Smatthias.ringwald //  notify client/protocol handler
10858de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
10958de5610Smatthias.ringwald     if (channel->packet_handler) {
11058de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
11158de5610Smatthias.ringwald     } else {
11236944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
11358de5610Smatthias.ringwald     }
11458de5610Smatthias.ringwald }
11558de5610Smatthias.ringwald 
11658de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1179893b714Smatthias.ringwald     uint8_t event[21];
11858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
11958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12058de5610Smatthias.ringwald     event[2] = status;
12158de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
12258de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
12358de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
12458de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
12558de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1264c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1274c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
12858de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
12958de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13058de5610Smatthias.ringwald }
13158de5610Smatthias.ringwald 
13258de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
13358de5610Smatthias.ringwald     uint8_t event[4];
13458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
13558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13658de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
13758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13858de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13958de5610Smatthias.ringwald }
14058de5610Smatthias.ringwald 
14158de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
14258de5610Smatthias.ringwald     uint8_t event[16];
14358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
14458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14558de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
14658de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
14758de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
14858de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
14958de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
15058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15158de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1520af41d30Smatthias.ringwald }
153808a48abSmatthias.ringwald 
1546218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1556218e6f1Smatthias.ringwald     // track credits
1566218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1577b5fbe1fSmatthias.ringwald     // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1586218e6f1Smatthias.ringwald 
1596218e6f1Smatthias.ringwald     uint8_t event[5];
1606218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1616218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1626218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1636218e6f1Smatthias.ringwald     event[4] = credits;
1646218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1656218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1666218e6f1Smatthias.ringwald }
1676218e6f1Smatthias.ringwald 
168808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
169808a48abSmatthias.ringwald     new_credits_blocked = blocked;
170808a48abSmatthias.ringwald }
171808a48abSmatthias.ringwald 
17240d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
173808a48abSmatthias.ringwald 
174808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
175808a48abSmatthias.ringwald 
1768d371091Smatthias.ringwald     linked_item_t *it;
1778d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1788d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1798d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1800e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1814c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1828d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1838d371091Smatthias.ringwald         }
1848d371091Smatthias.ringwald     }
1858d371091Smatthias.ringwald }
1868d371091Smatthias.ringwald 
187b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
188f62db1e3Smatthias.ringwald     linked_item_t *it;
189f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1908d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
191b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
192f62db1e3Smatthias.ringwald             return channel;
193f62db1e3Smatthias.ringwald         }
194f62db1e3Smatthias.ringwald     }
195f62db1e3Smatthias.ringwald     return NULL;
196f62db1e3Smatthias.ringwald }
197f62db1e3Smatthias.ringwald 
1986b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
1996b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2006b1fde37Smatthias.ringwald     if (!channel) return 0;
2016b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2026b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2036b1fde37Smatthias.ringwald }
2046b1fde37Smatthias.ringwald 
20596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
20696cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
20796cbd662Smatthias.ringwald     if (channel) {
20896cbd662Smatthias.ringwald         return channel->remote_mtu;
20996cbd662Smatthias.ringwald     }
21096cbd662Smatthias.ringwald     return 0;
21196cbd662Smatthias.ringwald }
21296cbd662Smatthias.ringwald 
21358de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
214b1d43497Smatthias.ringwald 
215b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
216b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
217b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
218b1d43497Smatthias.ringwald     }
219b1d43497Smatthias.ringwald 
2207b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
221b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
22258de5610Smatthias.ringwald     va_list argptr;
22358de5610Smatthias.ringwald     va_start(argptr, identifier);
224816c0598Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
22558de5610Smatthias.ringwald     va_end(argptr);
2267b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
227816c0598Smatthias.ringwald     return hci_send_acl_packet(acl_buffer, len);
22858de5610Smatthias.ringwald }
22958de5610Smatthias.ringwald 
230b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
231b1d43497Smatthias.ringwald     return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
232b1d43497Smatthias.ringwald }
2336218e6f1Smatthias.ringwald 
234b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
235b1d43497Smatthias.ringwald 
236b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
237b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
238808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2398ea03fa5Smatthias.ringwald     }
2406218e6f1Smatthias.ringwald 
24158de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
242b1d43497Smatthias.ringwald     if (!channel) {
243b1d43497Smatthias.ringwald         log_error("l2cap_send_internal no channel for cid %u\n", local_cid);
244b1d43497Smatthias.ringwald         return -1;   // TODO: define error
2456218e6f1Smatthias.ringwald     }
2466218e6f1Smatthias.ringwald 
247b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
248b1d43497Smatthias.ringwald         log_error("l2cap_send_internal cid %u, no credits!\n", local_cid);
249b1d43497Smatthias.ringwald         return -1;  // TODO: define error
250b1d43497Smatthias.ringwald     }
251b1d43497Smatthias.ringwald 
252b1d43497Smatthias.ringwald     --channel->packets_granted;
253b1d43497Smatthias.ringwald 
254b1d43497Smatthias.ringwald     log_debug("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
255b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
256b1d43497Smatthias.ringwald 
257b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
258b1d43497Smatthias.ringwald 
25958de5610Smatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
26058de5610Smatthias.ringwald     bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
26158de5610Smatthias.ringwald     // 2 - ACL length
26258de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
26358de5610Smatthias.ringwald     // 4 - L2CAP packet length
26458de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
26558de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
26658de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
26758de5610Smatthias.ringwald     // send
268b1d43497Smatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
26991b99603Smatthias.ringwald 
27091b99603Smatthias.ringwald     l2cap_hand_out_credits();
27191b99603Smatthias.ringwald 
2726218e6f1Smatthias.ringwald     return err;
27358de5610Smatthias.ringwald }
27458de5610Smatthias.ringwald 
275b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
276b1d43497Smatthias.ringwald 
277b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
278b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
279b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
280b1d43497Smatthias.ringwald     }
281b1d43497Smatthias.ringwald 
282b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
283b1d43497Smatthias.ringwald 
284b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
285b1d43497Smatthias.ringwald 
286b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
287b1d43497Smatthias.ringwald }
288b1d43497Smatthias.ringwald 
289b1d43497Smatthias.ringwald 
2908158c421Smatthias.ringwald // MARK: L2CAP_RUN
2912cd0be45Smatthias.ringwald // process outstanding signaling tasks
2922cd0be45Smatthias.ringwald void l2cap_run(void){
2932b83fb7dSmatthias.ringwald 
2942b83fb7dSmatthias.ringwald     // check pending signaling responses
2952b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
2962b83fb7dSmatthias.ringwald 
29702b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
2982b83fb7dSmatthias.ringwald 
2992b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3002b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3012b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
3022b360848Smatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST
3032b83fb7dSmatthias.ringwald 
3042b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3052b360848Smatthias.ringwald             case CONNECTION_REQUEST:
3062b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
3072b360848Smatthias.ringwald                 break;
3082b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3092b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3102b83fb7dSmatthias.ringwald                 break;
3112b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3122b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3132b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3142b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3152b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3162b83fb7dSmatthias.ringwald                 } else {
3172b83fb7dSmatthias.ringwald                     // all other types are not supported
3182b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3192b83fb7dSmatthias.ringwald                 }
3202b83fb7dSmatthias.ringwald                 break;
3212b83fb7dSmatthias.ringwald             default:
3222b83fb7dSmatthias.ringwald                 // should not happen
3232b83fb7dSmatthias.ringwald                 break;
3242b83fb7dSmatthias.ringwald         }
3252b83fb7dSmatthias.ringwald 
3262b83fb7dSmatthias.ringwald         // remove first item
3272b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3282b83fb7dSmatthias.ringwald         int i;
3292b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
33076115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
3312b83fb7dSmatthias.ringwald         }
3322b83fb7dSmatthias.ringwald     }
3332b83fb7dSmatthias.ringwald 
334ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3352cd0be45Smatthias.ringwald     linked_item_t *it;
3362cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3372cd0be45Smatthias.ringwald 
33802b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
33902b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3402cd0be45Smatthias.ringwald 
3412cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
3426e6710ebSmatthias.ringwald 
3437b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
3446e6710ebSmatthias.ringwald 
34556081214Smatthias.ringwald 
3462cd0be45Smatthias.ringwald         switch (channel->state){
3472cd0be45Smatthias.ringwald 
34802b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
34964472d52Smatthias.ringwald                 // send connection request - set state first
35064472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
35102b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
3528f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
35302b22dc4Smatthias.ringwald                 break;
35402b22dc4Smatthias.ringwald 
355e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
356b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
357e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
358e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
359d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
360e7ff783cSmatthias.ringwald                 break;
361e7ff783cSmatthias.ringwald 
362552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
363fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
36473cf2b3dSmatthias.ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
3652a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
366552d92a1Smatthias.ringwald                 break;
367552d92a1Smatthias.ringwald 
3686fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
3696fdcc387Smatthias.ringwald                 // success, start l2cap handshake
370b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
3716fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
3722a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
3736fdcc387Smatthias.ringwald                 break;
3746fdcc387Smatthias.ringwald 
375fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
37673cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
37773cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
37873cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP;
379fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
380fa8473a4Smatthias.ringwald                 }
38173cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
38273cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
38373cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ;
384b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
385ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
386ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
387ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
388b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
389fa8473a4Smatthias.ringwald                 }
390fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
391552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
392552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
393552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
394fa8473a4Smatthias.ringwald                 }
395552d92a1Smatthias.ringwald                 break;
396552d92a1Smatthias.ringwald 
397e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
398b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
399e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
400e7ff783cSmatthias.ringwald                 break;
401e7ff783cSmatthias.ringwald 
402e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
403b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4042cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4052a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4062cd0be45Smatthias.ringwald                 break;
4072cd0be45Smatthias.ringwald             default:
4082cd0be45Smatthias.ringwald                 break;
4092cd0be45Smatthias.ringwald         }
4102cd0be45Smatthias.ringwald     }
4112cd0be45Smatthias.ringwald }
4122cd0be45Smatthias.ringwald 
4134aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
414816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
415fa8c92f6Smatthias.ringwald }
416fa8c92f6Smatthias.ringwald 
4171e6aba47Smatthias.ringwald // open outgoing L2CAP channel
41815470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
41915470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4201e6aba47Smatthias.ringwald 
4211e6aba47Smatthias.ringwald     // alloc structure
422d3a9df87Smatthias.ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
4232b360848Smatthias.ringwald     if (!chan) {
4242b360848Smatthias.ringwald         // emit error event
4252b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
4262b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
4272b360848Smatthias.ringwald         dummy_channel.psm = psm;
4282b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
4292b360848Smatthias.ringwald         return;
4302b360848Smatthias.ringwald     }
4311d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
4322985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
4332985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
4349775e25bSmatthias.ringwald     }
4359775e25bSmatthias.ringwald 
4361e6aba47Smatthias.ringwald     // fill in
4371e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4381e6aba47Smatthias.ringwald     chan->psm = psm;
4391e6aba47Smatthias.ringwald     chan->handle = 0;
4401e6aba47Smatthias.ringwald     chan->connection = connection;
4416b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4422784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
44315470d27Smatthias.ringwald     chan->local_mtu = mtu;
4446218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4456218e6f1Smatthias.ringwald 
4461e6aba47Smatthias.ringwald     // set initial state
44702b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
44873cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
449b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
450b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
4511e6aba47Smatthias.ringwald 
4521e6aba47Smatthias.ringwald     // add to connections list
4531e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
4541e6aba47Smatthias.ringwald 
45502b22dc4Smatthias.ringwald     l2cap_run();
45643625864Smatthias.ringwald }
45743625864Smatthias.ringwald 
458b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
459b35f641cSmatthias.ringwald     // find channel for local_cid
460b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
461f62db1e3Smatthias.ringwald     if (channel) {
462e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
463f62db1e3Smatthias.ringwald     }
4642cd0be45Smatthias.ringwald     // process
4652cd0be45Smatthias.ringwald     l2cap_run();
46643625864Smatthias.ringwald }
4671e6aba47Smatthias.ringwald 
468afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
46915ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
47015ec09bbSmatthias.ringwald     while (it->next){
47115ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
472b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
47302b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
474afde0c52Smatthias.ringwald                 // failure, forward error code
475afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
476afde0c52Smatthias.ringwald                 // discard channel
47715ec09bbSmatthias.ringwald                 it->next = it->next->next;
478d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
479afde0c52Smatthias.ringwald             }
48015ec09bbSmatthias.ringwald         } else {
48115ec09bbSmatthias.ringwald             it = it->next;
482afde0c52Smatthias.ringwald         }
483afde0c52Smatthias.ringwald     }
484afde0c52Smatthias.ringwald }
485afde0c52Smatthias.ringwald 
486afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
487afde0c52Smatthias.ringwald     linked_item_t *it;
488afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
489afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
490afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
49102b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
492b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
4936fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
494afde0c52Smatthias.ringwald                 channel->handle = handle;
495b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
496afde0c52Smatthias.ringwald             }
497afde0c52Smatthias.ringwald         }
498afde0c52Smatthias.ringwald     }
4996fdcc387Smatthias.ringwald     // process
5006fdcc387Smatthias.ringwald     l2cap_run();
501afde0c52Smatthias.ringwald }
502b448a0e7Smatthias.ringwald 
503afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
504afde0c52Smatthias.ringwald 
505afde0c52Smatthias.ringwald     bd_addr_t address;
506afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5076e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
50836944dffSmatthias.ringwald     linked_item_t *it;
5092d00edd4Smatthias.ringwald     int hci_con_used;
510afde0c52Smatthias.ringwald 
511afde0c52Smatthias.ringwald     switch(packet[0]){
512afde0c52Smatthias.ringwald 
513afde0c52Smatthias.ringwald         // handle connection complete events
514afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
515afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
516afde0c52Smatthias.ringwald             if (packet[2] == 0){
517afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
518afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
519afde0c52Smatthias.ringwald             } else {
520afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
521afde0c52Smatthias.ringwald             }
522afde0c52Smatthias.ringwald             break;
523afde0c52Smatthias.ringwald 
524afde0c52Smatthias.ringwald         // handle successful create connection cancel command
525afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
526afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
527afde0c52Smatthias.ringwald                 if (packet[5] == 0){
528afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
529afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
530afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
53103cfbabcSmatthias.ringwald                 }
5321e6aba47Smatthias.ringwald             }
53339d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
53439d59809Smatthias.ringwald             break;
53539d59809Smatthias.ringwald 
53639d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
53739d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
538afde0c52Smatthias.ringwald             break;
53927a923d0Smatthias.ringwald 
5401e6aba47Smatthias.ringwald         // handle disconnection complete events
541afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
54227a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
543afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
54415ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
54515ec09bbSmatthias.ringwald             while (it->next){
54639ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
54727a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
54815ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
5492060cf14Smatthias.ringwald                     it->next = it->next->next;
55015ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
551d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
55215ec09bbSmatthias.ringwald                 } else {
55315ec09bbSmatthias.ringwald                     it = it->next;
55427a923d0Smatthias.ringwald                 }
55527a923d0Smatthias.ringwald             }
556afde0c52Smatthias.ringwald             break;
557fcadd0caSmatthias.ringwald 
5586218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
55902b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
5608d371091Smatthias.ringwald             l2cap_hand_out_credits();
5616218e6f1Smatthias.ringwald             break;
5626218e6f1Smatthias.ringwald 
563ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
564afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
56536944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
56680ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
5672d00edd4Smatthias.ringwald             hci_con_used = 0;
568ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
569ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
570ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
5712d00edd4Smatthias.ringwald                     hci_con_used = 1;
572ee091cf1Smatthias.ringwald                 }
573ee091cf1Smatthias.ringwald             }
5742d00edd4Smatthias.ringwald             if (hci_con_used) break;
57532ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
5769edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
577afde0c52Smatthias.ringwald             break;
578ee091cf1Smatthias.ringwald 
5796e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
5806e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
5816e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
5826e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
5836e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
5846e6710ebSmatthias.ringwald                 }
5856e6710ebSmatthias.ringwald             }
5866e6710ebSmatthias.ringwald             break;
5876e6710ebSmatthias.ringwald 
588afde0c52Smatthias.ringwald         default:
589afde0c52Smatthias.ringwald             break;
590afde0c52Smatthias.ringwald     }
591afde0c52Smatthias.ringwald 
592afde0c52Smatthias.ringwald     // pass on
593b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
5941e6aba47Smatthias.ringwald }
5951e6aba47Smatthias.ringwald 
596afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
597b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
598e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
599e7ff783cSmatthias.ringwald     l2cap_run();
60084836b65Smatthias.ringwald }
60184836b65Smatthias.ringwald 
6022b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
6032b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
6042b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
6052b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
6062b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
6072b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
6082b360848Smatthias.ringwald         signaling_responses_pending++;
6092b360848Smatthias.ringwald         l2cap_run();
6102b360848Smatthias.ringwald     }
6112b360848Smatthias.ringwald }
6122b360848Smatthias.ringwald 
613b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
614645658c9Smatthias.ringwald 
6157b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
616645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
617645658c9Smatthias.ringwald     if (!service) {
618645658c9Smatthias.ringwald         // 0x0002 PSM not supported
6192b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
620645658c9Smatthias.ringwald         return;
621645658c9Smatthias.ringwald     }
622645658c9Smatthias.ringwald 
623645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
624645658c9Smatthias.ringwald     if (!hci_connection) {
6252b360848Smatthias.ringwald         //
6267d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
627645658c9Smatthias.ringwald         return;
628645658c9Smatthias.ringwald     }
629645658c9Smatthias.ringwald     // alloc structure
6307b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
631d3a9df87Smatthias.ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
6322b360848Smatthias.ringwald     if (!channel){
6332b360848Smatthias.ringwald         // 0x0004 No resources available
6342b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
6352b360848Smatthias.ringwald         return;
6362b360848Smatthias.ringwald     }
637645658c9Smatthias.ringwald 
638645658c9Smatthias.ringwald     // fill in
639169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
640169f8b28Smatthias.ringwald     channel->psm = psm;
641169f8b28Smatthias.ringwald     channel->handle = handle;
642169f8b28Smatthias.ringwald     channel->connection = service->connection;
643f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
644b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
645b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
646fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6470a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
648761b0451Smatthias.ringwald     channel->packets_granted = 0;
649b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
650645658c9Smatthias.ringwald 
6511d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
6522985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
6532985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
6549775e25bSmatthias.ringwald     }
6559775e25bSmatthias.ringwald 
656645658c9Smatthias.ringwald     // set initial state
657e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
65873cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
659e405ae81Smatthias.ringwald 
660645658c9Smatthias.ringwald     // add to connections list
661169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
662645658c9Smatthias.ringwald 
663e405ae81Smatthias.ringwald     // emit incoming connection request
664e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
665e405ae81Smatthias.ringwald }
666645658c9Smatthias.ringwald 
667b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
668b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
669e405ae81Smatthias.ringwald     if (!channel) {
6707d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
671e405ae81Smatthias.ringwald         return;
672e405ae81Smatthias.ringwald     }
673e405ae81Smatthias.ringwald 
674552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
675e405ae81Smatthias.ringwald 
676552d92a1Smatthias.ringwald     // process
677552d92a1Smatthias.ringwald     l2cap_run();
678e405ae81Smatthias.ringwald }
679645658c9Smatthias.ringwald 
680b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
681b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
682e405ae81Smatthias.ringwald     if (!channel) {
6837d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
684e405ae81Smatthias.ringwald         return;
685e405ae81Smatthias.ringwald     }
686e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
687e7ff783cSmatthias.ringwald     channel->reason = reason;
688e7ff783cSmatthias.ringwald     l2cap_run();
689645658c9Smatthias.ringwald }
690645658c9Smatthias.ringwald 
6912784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
692b1988dceSmatthias.ringwald 
693b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
694b1988dceSmatthias.ringwald 
6952784b77dSmatthias.ringwald     // accept the other's configuration options
6963de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
6973de7c0caSmatthias.ringwald     uint16_t pos     = 8;
6983de7c0caSmatthias.ringwald     while (pos < end_pos){
6991dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7001dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7011dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7021dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7031dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
7047b5fbe1fSmatthias.ringwald             // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7051dc511deSmatthias.ringwald         }
7061dc511deSmatthias.ringwald         pos += length;
7071dc511deSmatthias.ringwald     }
7082784b77dSmatthias.ringwald }
7092784b77dSmatthias.ringwald 
710fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
7117b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
71273cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
71373cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
714fa8473a4Smatthias.ringwald     return 1;
715fa8473a4Smatthias.ringwald }
716fa8473a4Smatthias.ringwald 
717fa8473a4Smatthias.ringwald 
71800d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7191e6aba47Smatthias.ringwald 
72000d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
72100d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
72238e5900eSmatthias.ringwald     uint16_t result = 0;
7231e6aba47Smatthias.ringwald 
7247b5fbe1fSmatthias.ringwald     log_info("signaling handler code %u, state %u\n", code, channel->state);
725b35f641cSmatthias.ringwald 
7269a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7279a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7289a011532Smatthias.ringwald         switch (channel->state){
729fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7309a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7312b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7329a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7339a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7349a011532Smatthias.ringwald                 break;
7359a011532Smatthias.ringwald 
7369a011532Smatthias.ringwald             default:
7379a011532Smatthias.ringwald                 // ignore in other states
7389a011532Smatthias.ringwald                 break;
7399a011532Smatthias.ringwald         }
7409a011532Smatthias.ringwald         return;
7419a011532Smatthias.ringwald     }
7429a011532Smatthias.ringwald 
74356081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
7441e6aba47Smatthias.ringwald     switch (channel->state) {
7451e6aba47Smatthias.ringwald 
7461e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7471e6aba47Smatthias.ringwald             switch (code){
7481e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
74900d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
75038e5900eSmatthias.ringwald                     switch (result) {
75138e5900eSmatthias.ringwald                         case 0:
752169f8b28Smatthias.ringwald                             // successful connection
75300d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
754fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
75573cf2b3dSmatthias.ringwald                             channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
75638e5900eSmatthias.ringwald                             break;
75738e5900eSmatthias.ringwald                         case 1:
75838e5900eSmatthias.ringwald                             // connection pending. get some coffee
75938e5900eSmatthias.ringwald                             break;
76038e5900eSmatthias.ringwald                         default:
761eb920dbeSmatthias.ringwald                             // channel closed
762eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
763eb920dbeSmatthias.ringwald 
764f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
76538e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
766eb920dbeSmatthias.ringwald 
767eb920dbeSmatthias.ringwald                             // drop link key if security block
768eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
769eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
770eb920dbeSmatthias.ringwald                             }
771eb920dbeSmatthias.ringwald 
772eb920dbeSmatthias.ringwald                             // discard channel
773eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
774d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
77538e5900eSmatthias.ringwald                             break;
7761e6aba47Smatthias.ringwald                     }
7771e6aba47Smatthias.ringwald                     break;
77838e5900eSmatthias.ringwald 
77938e5900eSmatthias.ringwald                 default:
7801e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
78138e5900eSmatthias.ringwald                     break;
7821e6aba47Smatthias.ringwald             }
7831e6aba47Smatthias.ringwald             break;
7841e6aba47Smatthias.ringwald 
785fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
786ae280e73Smatthias.ringwald             switch (code) {
787ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
78873cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ;
78973cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
790ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
791ae280e73Smatthias.ringwald                     break;
7921e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
79373cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP;
7945a67bd4aSmatthias.ringwald                     break;
7955a67bd4aSmatthias.ringwald                 default:
7965a67bd4aSmatthias.ringwald                     break;
7971e6aba47Smatthias.ringwald             }
798fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
799fa8473a4Smatthias.ringwald                 // for open:
8005a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
801fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8026218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
803c8e4258aSmatthias.ringwald             }
804c8e4258aSmatthias.ringwald             break;
805f62db1e3Smatthias.ringwald 
806f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
807f62db1e3Smatthias.ringwald             switch (code) {
808f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
80927a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
81027a923d0Smatthias.ringwald                     break;
8115a67bd4aSmatthias.ringwald                 default:
8125a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8135a67bd4aSmatthias.ringwald                     break;
81427a923d0Smatthias.ringwald             }
81527a923d0Smatthias.ringwald             break;
81684836b65Smatthias.ringwald 
81784836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
81884836b65Smatthias.ringwald             // @TODO handle incoming requests
81984836b65Smatthias.ringwald             break;
82084836b65Smatthias.ringwald 
82184836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
82284836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
82384836b65Smatthias.ringwald             break;
82410642e45Smatthias.ringwald         default:
82510642e45Smatthias.ringwald             break;
82627a923d0Smatthias.ringwald     }
8277b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
82827a923d0Smatthias.ringwald }
82927a923d0Smatthias.ringwald 
83000d93d79Smatthias.ringwald 
83100d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
83200d93d79Smatthias.ringwald 
83300d93d79Smatthias.ringwald     // get code, signalind identifier and command len
83400d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
83500d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
83600d93d79Smatthias.ringwald 
83700d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
83800d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
83900d93d79Smatthias.ringwald         return;
84000d93d79Smatthias.ringwald     }
84100d93d79Smatthias.ringwald 
84200d93d79Smatthias.ringwald     // general commands without an assigned channel
84300d93d79Smatthias.ringwald     switch(code) {
84400d93d79Smatthias.ringwald 
84500d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
84600d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
84700d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
84800d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8492b83fb7dSmatthias.ringwald             return;
85000d93d79Smatthias.ringwald         }
85100d93d79Smatthias.ringwald 
8522b360848Smatthias.ringwald         case ECHO_REQUEST:
8532b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
8542b83fb7dSmatthias.ringwald             return;
85500d93d79Smatthias.ringwald 
85600d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
85700d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
8582b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
8592b83fb7dSmatthias.ringwald             return;
86000d93d79Smatthias.ringwald         }
86100d93d79Smatthias.ringwald 
86200d93d79Smatthias.ringwald         default:
86300d93d79Smatthias.ringwald             break;
86400d93d79Smatthias.ringwald     }
86500d93d79Smatthias.ringwald 
86600d93d79Smatthias.ringwald 
86700d93d79Smatthias.ringwald     // Get potential destination CID
86800d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
86900d93d79Smatthias.ringwald 
87000d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
87100d93d79Smatthias.ringwald     linked_item_t *it;
87200d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
87300d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
87400d93d79Smatthias.ringwald         if (channel->handle == handle) {
87500d93d79Smatthias.ringwald             if (code & 1) {
876b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
877b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
87800d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8794e32727eSmatthias.ringwald                     break;
88000d93d79Smatthias.ringwald                 }
88100d93d79Smatthias.ringwald             } else {
882b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
88300d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
88400d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8854e32727eSmatthias.ringwald                     break;
88600d93d79Smatthias.ringwald                 }
88700d93d79Smatthias.ringwald             }
88800d93d79Smatthias.ringwald         }
88900d93d79Smatthias.ringwald     }
89000d93d79Smatthias.ringwald }
89100d93d79Smatthias.ringwald 
89200d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
89300d93d79Smatthias.ringwald 
89400d93d79Smatthias.ringwald     // Get Channel ID
89500d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
89600d93d79Smatthias.ringwald 
89700d93d79Smatthias.ringwald     // Signaling Packet?
89800d93d79Smatthias.ringwald     if (channel_id == 1) {
89900d93d79Smatthias.ringwald 
90000d93d79Smatthias.ringwald         // Get Connection
90100d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
90200d93d79Smatthias.ringwald 
90300d93d79Smatthias.ringwald         uint16_t command_offset = 8;
90400d93d79Smatthias.ringwald         while (command_offset < size) {
90500d93d79Smatthias.ringwald 
90600d93d79Smatthias.ringwald             // handle signaling commands
90700d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
90800d93d79Smatthias.ringwald 
90900d93d79Smatthias.ringwald             // increment command_offset
91000d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
91100d93d79Smatthias.ringwald         }
91264472d52Smatthias.ringwald 
91364472d52Smatthias.ringwald         l2cap_run();
91464472d52Smatthias.ringwald 
91500d93d79Smatthias.ringwald         return;
91600d93d79Smatthias.ringwald     }
91700d93d79Smatthias.ringwald 
91800d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
91900d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
92000d93d79Smatthias.ringwald     if (channel) {
92158de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
92200d93d79Smatthias.ringwald     }
92300d93d79Smatthias.ringwald }
92400d93d79Smatthias.ringwald 
9252718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9262718e2e7Smatthias.ringwald     switch (packet_type) {
9272718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9282718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9292718e2e7Smatthias.ringwald             break;
9302718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9312718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9322718e2e7Smatthias.ringwald             break;
9332718e2e7Smatthias.ringwald         default:
9342718e2e7Smatthias.ringwald             break;
9352718e2e7Smatthias.ringwald     }
9362718e2e7Smatthias.ringwald }
93700d93d79Smatthias.ringwald 
93815ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
93927a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
940f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
941f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
942f62db1e3Smatthias.ringwald     // discard channel
943f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
944d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
945c8e4258aSmatthias.ringwald }
9461e6aba47Smatthias.ringwald 
9479d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
948c52bf64dSmatthias.ringwald     linked_item_t *it;
9499d9bbc01Smatthias.ringwald 
9509d9bbc01Smatthias.ringwald     // close open channels
9519d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9529d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9539d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9549d9bbc01Smatthias.ringwald             return service;
9559d9bbc01Smatthias.ringwald         };
9569d9bbc01Smatthias.ringwald     }
9579d9bbc01Smatthias.ringwald     return NULL;
9589d9bbc01Smatthias.ringwald }
9599d9bbc01Smatthias.ringwald 
96036944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
9614bb582b6Smatthias.ringwald     // check for alread registered psm
9624bb582b6Smatthias.ringwald     // TODO: emit error event
9639d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
964277abc2cSmatthias.ringwald     if (service) {
965277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
966277abc2cSmatthias.ringwald         return;
967277abc2cSmatthias.ringwald     }
9689d9bbc01Smatthias.ringwald 
9694bb582b6Smatthias.ringwald     // alloc structure
9704bb582b6Smatthias.ringwald     // TODO: emit error event
971d3a9df87Smatthias.ringwald     service = btstack_memory_l2cap_service_get();
972277abc2cSmatthias.ringwald     if (!service) {
973277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
974277abc2cSmatthias.ringwald         return;
975277abc2cSmatthias.ringwald     }
9769d9bbc01Smatthias.ringwald 
9779d9bbc01Smatthias.ringwald     // fill in
9789d9bbc01Smatthias.ringwald     service->psm = psm;
9799d9bbc01Smatthias.ringwald     service->mtu = mtu;
9809d9bbc01Smatthias.ringwald     service->connection = connection;
981d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
9829d9bbc01Smatthias.ringwald 
9839d9bbc01Smatthias.ringwald     // add to services list
9849d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
9859d9bbc01Smatthias.ringwald }
9869d9bbc01Smatthias.ringwald 
98736944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
9889d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
989037d6e48Smatthias.ringwald     if (!service) return;
9909d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
991d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
9929d9bbc01Smatthias.ringwald }
9939d9bbc01Smatthias.ringwald 
9949d9bbc01Smatthias.ringwald //
99536944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
9969d9bbc01Smatthias.ringwald     linked_item_t *it;
9979d9bbc01Smatthias.ringwald 
9983a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
999c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1000c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1001c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1002c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1003cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1004c52bf64dSmatthias.ringwald         }
1005c52bf64dSmatthias.ringwald     }
10069d9bbc01Smatthias.ringwald 
1007645658c9Smatthias.ringwald     // unregister services
100869025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
100969025de8Smatthias.ringwald     while (it->next) {
101069025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1011645658c9Smatthias.ringwald         if (service->connection == connection){
101269025de8Smatthias.ringwald             it->next = it->next->next;
1013d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
10148149d2c7Smatthias.ringwald         } else {
10158149d2c7Smatthias.ringwald             it = it->next;
1016645658c9Smatthias.ringwald         }
1017645658c9Smatthias.ringwald     }
1018cb8eb7e6Smatthias.ringwald 
1019cb8eb7e6Smatthias.ringwald     // process
1020cb8eb7e6Smatthias.ringwald     l2cap_run();
1021c52bf64dSmatthias.ringwald }
1022c52bf64dSmatthias.ringwald