xref: /btstack/src/l2cap.c (revision 4bb582b6c900ce90007a94e26c401226fab6db54)
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 
516f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
526f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
536f60b3f4Smatthias.ringwald 
542784b77dSmatthias.ringwald // minimum signaling MTU
552784b77dSmatthias.ringwald #define L2CAP_MINIMAL_MTU 48
561dc511deSmatthias.ringwald #define L2CAP_DEFAULT_MTU 672
572784b77dSmatthias.ringwald 
584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
604c744e21Smatthias.ringwald 
6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
622b360848Smatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 5
6339bda6d5Smatthias.ringwald 
6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6900d93d79Smatthias.ringwald 
7039bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
7139bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
7239bda6d5Smatthias.ringwald 
7339bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
742b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
752b83fb7dSmatthias.ringwald static int signaling_responses_pending;
762b83fb7dSmatthias.ringwald 
77d3a9df87Smatthias.ringwald // static buffers
78d3a9df87Smatthias.ringwald static uint8_t sig_buffer[L2CAP_MINIMAL_MTU];
79d3a9df87Smatthias.ringwald static uint8_t acl_buffer[HCI_ACL_3DH5_SIZE];
80d3a9df87Smatthias.ringwald 
811e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
829d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
8336944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
84808a48abSmatthias.ringwald static int new_credits_blocked = 0;
851e6aba47Smatthias.ringwald 
8639bda6d5Smatthias.ringwald // prototypes
87fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
88fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
89fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
90fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
91fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
92fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
9339bda6d5Smatthias.ringwald 
9439bda6d5Smatthias.ringwald 
951e6aba47Smatthias.ringwald void l2cap_init(){
96fcadd0caSmatthias.ringwald 
97808a48abSmatthias.ringwald     new_credits_blocked = 0;
982b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
99808a48abSmatthias.ringwald 
100fcadd0caSmatthias.ringwald     //
1012718e2e7Smatthias.ringwald     // register callback with HCI
102fcadd0caSmatthias.ringwald     //
1032718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
104fcadd0caSmatthias.ringwald }
105fcadd0caSmatthias.ringwald 
106fcadd0caSmatthias.ringwald 
107fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
10836944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
109fcadd0caSmatthias.ringwald }
11036944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
111b502e1b0Smatthias.ringwald     packet_handler = handler;
1121e6aba47Smatthias.ringwald }
1131e6aba47Smatthias.ringwald 
11458de5610Smatthias.ringwald //  notify client/protocol handler
11558de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
11658de5610Smatthias.ringwald     if (channel->packet_handler) {
11758de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
11858de5610Smatthias.ringwald     } else {
11936944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
12058de5610Smatthias.ringwald     }
12158de5610Smatthias.ringwald }
12258de5610Smatthias.ringwald 
12358de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1249893b714Smatthias.ringwald     uint8_t event[21];
12558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
12658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12758de5610Smatthias.ringwald     event[2] = status;
12858de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
12958de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
13058de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
13158de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
13258de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1334c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1344c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
13558de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13658de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13758de5610Smatthias.ringwald }
13858de5610Smatthias.ringwald 
13958de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
14058de5610Smatthias.ringwald     uint8_t event[4];
14158de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
14258de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14358de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
14458de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
14558de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
14658de5610Smatthias.ringwald }
14758de5610Smatthias.ringwald 
14858de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
14958de5610Smatthias.ringwald     uint8_t event[16];
15058de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
15158de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
15258de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
15358de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
15458de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
15558de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
15658de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
15758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15858de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1590af41d30Smatthias.ringwald }
160808a48abSmatthias.ringwald 
1616218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1626218e6f1Smatthias.ringwald     // track credits
1636218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1647b5fbe1fSmatthias.ringwald     // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1656218e6f1Smatthias.ringwald 
1666218e6f1Smatthias.ringwald     uint8_t event[5];
1676218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1686218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1696218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1706218e6f1Smatthias.ringwald     event[4] = credits;
1716218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1726218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1736218e6f1Smatthias.ringwald }
1746218e6f1Smatthias.ringwald 
175808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
176808a48abSmatthias.ringwald     new_credits_blocked = blocked;
177808a48abSmatthias.ringwald }
178808a48abSmatthias.ringwald 
17940d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
180808a48abSmatthias.ringwald 
181808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
182808a48abSmatthias.ringwald 
1838d371091Smatthias.ringwald     linked_item_t *it;
1848d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1858d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1868d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1870e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1884c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1898d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1908d371091Smatthias.ringwald         }
1918d371091Smatthias.ringwald     }
1928d371091Smatthias.ringwald }
1938d371091Smatthias.ringwald 
194b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
195f62db1e3Smatthias.ringwald     linked_item_t *it;
196f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1978d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
198b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
199f62db1e3Smatthias.ringwald             return channel;
200f62db1e3Smatthias.ringwald         }
201f62db1e3Smatthias.ringwald     }
202f62db1e3Smatthias.ringwald     return NULL;
203f62db1e3Smatthias.ringwald }
204f62db1e3Smatthias.ringwald 
2056b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2066b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2076b1fde37Smatthias.ringwald     if (!channel) return 0;
2086b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2096b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2106b1fde37Smatthias.ringwald }
2116b1fde37Smatthias.ringwald 
21296cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
21396cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
21496cbd662Smatthias.ringwald     if (channel) {
21596cbd662Smatthias.ringwald         return channel->remote_mtu;
21696cbd662Smatthias.ringwald     }
21796cbd662Smatthias.ringwald     return 0;
21896cbd662Smatthias.ringwald }
21996cbd662Smatthias.ringwald 
22058de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
2217b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
22258de5610Smatthias.ringwald     va_list argptr;
22358de5610Smatthias.ringwald     va_start(argptr, identifier);
22458de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
22558de5610Smatthias.ringwald     va_end(argptr);
2267b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
22758de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
22858de5610Smatthias.ringwald }
22958de5610Smatthias.ringwald 
2306218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
2316218e6f1Smatthias.ringwald 
2326218e6f1Smatthias.ringwald     // check for free places on BT module
2338ea03fa5Smatthias.ringwald     if (!hci_number_free_acl_slots()) {
2347b5fbe1fSmatthias.ringwald         log_info("l2cap_send_internal cid %u, BT module full <-----\n", local_cid);
235808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2368ea03fa5Smatthias.ringwald     }
2376218e6f1Smatthias.ringwald     int err = 0;
2386218e6f1Smatthias.ringwald 
23958de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
24058de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
24158de5610Smatthias.ringwald     if (channel) {
2426218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
2436218e6f1Smatthias.ringwald             --channel->packets_granted;
2447b5fbe1fSmatthias.ringwald             // log_info("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
245808a48abSmatthias.ringwald             //        local_cid, channel->handle, channel->packets_granted);
2466218e6f1Smatthias.ringwald         } else {
2477d67539fSmatthias.ringwald             log_error("l2cap_send_internal cid %u, no credits!\n", local_cid);
2486218e6f1Smatthias.ringwald         }
2496218e6f1Smatthias.ringwald 
25058de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
25158de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
25258de5610Smatthias.ringwald         // 2 - ACL length
25358de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
25458de5610Smatthias.ringwald         // 4 - L2CAP packet length
25558de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
25658de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
25758de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
25858de5610Smatthias.ringwald         // 8 - data
25958de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
2606218e6f1Smatthias.ringwald 
26158de5610Smatthias.ringwald         // send
2626218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
26358de5610Smatthias.ringwald     }
26491b99603Smatthias.ringwald 
26591b99603Smatthias.ringwald     l2cap_hand_out_credits();
26691b99603Smatthias.ringwald 
2676218e6f1Smatthias.ringwald     return err;
26858de5610Smatthias.ringwald }
26958de5610Smatthias.ringwald 
2708158c421Smatthias.ringwald // MARK: L2CAP_RUN
2712cd0be45Smatthias.ringwald // process outstanding signaling tasks
2722cd0be45Smatthias.ringwald void l2cap_run(void){
2732b83fb7dSmatthias.ringwald 
2742b83fb7dSmatthias.ringwald     // check pending signaling responses
2752b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
2762b83fb7dSmatthias.ringwald 
27702b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
2782b83fb7dSmatthias.ringwald 
2792b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
2802b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
2812b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
2822b360848Smatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST
2832b83fb7dSmatthias.ringwald 
2842b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
2852b360848Smatthias.ringwald             case CONNECTION_REQUEST:
2862b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
2872b360848Smatthias.ringwald                 break;
2882b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
2892b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
2902b83fb7dSmatthias.ringwald                 break;
2912b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
2922b83fb7dSmatthias.ringwald                 if (infoType == 2) {
2932b83fb7dSmatthias.ringwald                     uint32_t features = 0;
2942b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
2952b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
2962b83fb7dSmatthias.ringwald                 } else {
2972b83fb7dSmatthias.ringwald                     // all other types are not supported
2982b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
2992b83fb7dSmatthias.ringwald                 }
3002b83fb7dSmatthias.ringwald                 break;
3012b83fb7dSmatthias.ringwald             default:
3022b83fb7dSmatthias.ringwald                 // should not happen
3032b83fb7dSmatthias.ringwald                 break;
3042b83fb7dSmatthias.ringwald         }
3052b83fb7dSmatthias.ringwald 
3062b83fb7dSmatthias.ringwald         // remove first item
3072b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3082b83fb7dSmatthias.ringwald         int i;
3092b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
3102b83fb7dSmatthias.ringwald             signaling_responses[i] = signaling_responses[i+1];
3112b83fb7dSmatthias.ringwald         }
3122b83fb7dSmatthias.ringwald     }
3132b83fb7dSmatthias.ringwald 
314ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3152cd0be45Smatthias.ringwald     linked_item_t *it;
3162cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3172cd0be45Smatthias.ringwald 
31802b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
31902b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3202cd0be45Smatthias.ringwald 
3212cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
3226e6710ebSmatthias.ringwald 
3237b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
3246e6710ebSmatthias.ringwald 
3252cd0be45Smatthias.ringwald         switch (channel->state){
3262cd0be45Smatthias.ringwald 
32702b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
32864472d52Smatthias.ringwald                 // send connection request - set state first
32964472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
33002b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
33102b22dc4Smatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, 0xcc18, 0, 0, 0, 1);
33202b22dc4Smatthias.ringwald                 break;
33302b22dc4Smatthias.ringwald 
334e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
335b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
336e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
337e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
338d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
339e7ff783cSmatthias.ringwald                 break;
340e7ff783cSmatthias.ringwald 
341552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
342fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
34373cf2b3dSmatthias.ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
3442a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
345552d92a1Smatthias.ringwald                 break;
346552d92a1Smatthias.ringwald 
3476fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
3486fdcc387Smatthias.ringwald                 // success, start l2cap handshake
349b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
3506fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
3512a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
3526fdcc387Smatthias.ringwald                 break;
3536fdcc387Smatthias.ringwald 
354fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
35573cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
35673cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
35773cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP;
358fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
359fa8473a4Smatthias.ringwald                 }
36073cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
36173cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
36273cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ;
363b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
364ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
365ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
366ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
367b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
368fa8473a4Smatthias.ringwald                 }
369fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
370552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
371552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
372552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
373fa8473a4Smatthias.ringwald                 }
374552d92a1Smatthias.ringwald                 break;
375552d92a1Smatthias.ringwald 
376e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
377b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
378e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
379e7ff783cSmatthias.ringwald                 break;
380e7ff783cSmatthias.ringwald 
381e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
382b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
3832cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
3842a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
3852cd0be45Smatthias.ringwald                 break;
3862cd0be45Smatthias.ringwald             default:
3872cd0be45Smatthias.ringwald                 break;
3882cd0be45Smatthias.ringwald         }
3892cd0be45Smatthias.ringwald     }
3902cd0be45Smatthias.ringwald }
3912cd0be45Smatthias.ringwald 
392fa8c92f6Smatthias.ringwald static uint16_t l2cap_max_l2cap_mtu(void){
393fa8c92f6Smatthias.ringwald     return hci_max_acl_data_packet_length()-4;  // 4 bytes for L2CAP header
394fa8c92f6Smatthias.ringwald }
395fa8c92f6Smatthias.ringwald 
3961e6aba47Smatthias.ringwald // open outgoing L2CAP channel
39715470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
39815470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
3991e6aba47Smatthias.ringwald 
4001e6aba47Smatthias.ringwald     // alloc structure
401d3a9df87Smatthias.ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
4022b360848Smatthias.ringwald     if (!chan) {
4032b360848Smatthias.ringwald         // emit error event
4042b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
4052b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
4062b360848Smatthias.ringwald         dummy_channel.psm = psm;
4072b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
4082b360848Smatthias.ringwald         return;
4092b360848Smatthias.ringwald     }
4101d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
411fa8c92f6Smatthias.ringwald     if (mtu > l2cap_max_l2cap_mtu()) {
412fa8c92f6Smatthias.ringwald         mtu = l2cap_max_l2cap_mtu();
4139775e25bSmatthias.ringwald     }
4149775e25bSmatthias.ringwald 
4151e6aba47Smatthias.ringwald     // fill in
4161e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4171e6aba47Smatthias.ringwald     chan->psm = psm;
4181e6aba47Smatthias.ringwald     chan->handle = 0;
4191e6aba47Smatthias.ringwald     chan->connection = connection;
4206b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4212784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
42215470d27Smatthias.ringwald     chan->local_mtu = mtu;
4236218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4246218e6f1Smatthias.ringwald 
4251e6aba47Smatthias.ringwald     // set initial state
42602b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
42773cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
428b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
429b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
4301e6aba47Smatthias.ringwald 
4311e6aba47Smatthias.ringwald     // add to connections list
4321e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
4331e6aba47Smatthias.ringwald 
43402b22dc4Smatthias.ringwald     l2cap_run();
43543625864Smatthias.ringwald }
43643625864Smatthias.ringwald 
437b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
438b35f641cSmatthias.ringwald     // find channel for local_cid
439b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
440f62db1e3Smatthias.ringwald     if (channel) {
441e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
442f62db1e3Smatthias.ringwald     }
4432cd0be45Smatthias.ringwald     // process
4442cd0be45Smatthias.ringwald     l2cap_run();
44543625864Smatthias.ringwald }
4461e6aba47Smatthias.ringwald 
447afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
44815ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
44915ec09bbSmatthias.ringwald     while (it->next){
45015ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
451b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
45202b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
453afde0c52Smatthias.ringwald                 // failure, forward error code
454afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
455afde0c52Smatthias.ringwald                 // discard channel
45615ec09bbSmatthias.ringwald                 it->next = it->next->next;
457d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
458afde0c52Smatthias.ringwald             }
45915ec09bbSmatthias.ringwald         } else {
46015ec09bbSmatthias.ringwald             it = it->next;
461afde0c52Smatthias.ringwald         }
462afde0c52Smatthias.ringwald     }
463afde0c52Smatthias.ringwald }
464afde0c52Smatthias.ringwald 
465afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
466afde0c52Smatthias.ringwald     linked_item_t *it;
467afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
468afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
469afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
47002b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
471b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
4726fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
473afde0c52Smatthias.ringwald                 channel->handle = handle;
474b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
475afde0c52Smatthias.ringwald             }
476afde0c52Smatthias.ringwald         }
477afde0c52Smatthias.ringwald     }
4786fdcc387Smatthias.ringwald     // process
4796fdcc387Smatthias.ringwald     l2cap_run();
480afde0c52Smatthias.ringwald }
481b448a0e7Smatthias.ringwald 
482afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
483afde0c52Smatthias.ringwald 
484afde0c52Smatthias.ringwald     bd_addr_t address;
485afde0c52Smatthias.ringwald     hci_con_handle_t handle;
4866e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
48736944dffSmatthias.ringwald     linked_item_t *it;
4882d00edd4Smatthias.ringwald     int hci_con_used;
489afde0c52Smatthias.ringwald 
490afde0c52Smatthias.ringwald     switch(packet[0]){
491afde0c52Smatthias.ringwald 
492afde0c52Smatthias.ringwald         // handle connection complete events
493afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
494afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
495afde0c52Smatthias.ringwald             if (packet[2] == 0){
496afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
497afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
498afde0c52Smatthias.ringwald             } else {
499afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
500afde0c52Smatthias.ringwald             }
501afde0c52Smatthias.ringwald             break;
502afde0c52Smatthias.ringwald 
503afde0c52Smatthias.ringwald         // handle successful create connection cancel command
504afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
505afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
506afde0c52Smatthias.ringwald                 if (packet[5] == 0){
507afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
508afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
509afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
51003cfbabcSmatthias.ringwald                 }
5111e6aba47Smatthias.ringwald             }
51239d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
51339d59809Smatthias.ringwald             break;
51439d59809Smatthias.ringwald 
51539d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
51639d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
517afde0c52Smatthias.ringwald             break;
51827a923d0Smatthias.ringwald 
5191e6aba47Smatthias.ringwald         // handle disconnection complete events
520afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
52127a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
522afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
52315ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
52415ec09bbSmatthias.ringwald             while (it->next){
52539ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
52627a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
52715ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
5282060cf14Smatthias.ringwald                     it->next = it->next->next;
52915ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
530d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
53115ec09bbSmatthias.ringwald                 } else {
53215ec09bbSmatthias.ringwald                     it = it->next;
53327a923d0Smatthias.ringwald                 }
53427a923d0Smatthias.ringwald             }
535afde0c52Smatthias.ringwald             break;
536fcadd0caSmatthias.ringwald 
5376218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
53802b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
5398d371091Smatthias.ringwald             l2cap_hand_out_credits();
5406218e6f1Smatthias.ringwald             break;
5416218e6f1Smatthias.ringwald 
542ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
543afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
54436944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
54580ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
5462d00edd4Smatthias.ringwald             hci_con_used = 0;
547ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
548ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
549ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
5502d00edd4Smatthias.ringwald                     hci_con_used = 1;
551ee091cf1Smatthias.ringwald                 }
552ee091cf1Smatthias.ringwald             }
5532d00edd4Smatthias.ringwald             if (hci_con_used) break;
55432ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
5559edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
556afde0c52Smatthias.ringwald             break;
557ee091cf1Smatthias.ringwald 
5586e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
5596e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
5606e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
5616e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
5626e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
5636e6710ebSmatthias.ringwald                 }
5646e6710ebSmatthias.ringwald             }
5656e6710ebSmatthias.ringwald             break;
5666e6710ebSmatthias.ringwald 
567afde0c52Smatthias.ringwald         default:
568afde0c52Smatthias.ringwald             break;
569afde0c52Smatthias.ringwald     }
570afde0c52Smatthias.ringwald 
571afde0c52Smatthias.ringwald     // pass on
572b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
5731e6aba47Smatthias.ringwald }
5741e6aba47Smatthias.ringwald 
575afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
576b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
577e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
578e7ff783cSmatthias.ringwald     l2cap_run();
57984836b65Smatthias.ringwald }
58084836b65Smatthias.ringwald 
5812b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
5822b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
5832b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
5842b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
5852b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
5862b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
5872b360848Smatthias.ringwald         signaling_responses_pending++;
5882b360848Smatthias.ringwald         l2cap_run();
5892b360848Smatthias.ringwald     }
5902b360848Smatthias.ringwald }
5912b360848Smatthias.ringwald 
592b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
593645658c9Smatthias.ringwald 
5947b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
595645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
596645658c9Smatthias.ringwald     if (!service) {
597645658c9Smatthias.ringwald         // 0x0002 PSM not supported
5982b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
599645658c9Smatthias.ringwald         return;
600645658c9Smatthias.ringwald     }
601645658c9Smatthias.ringwald 
602645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
603645658c9Smatthias.ringwald     if (!hci_connection) {
6042b360848Smatthias.ringwald         //
6057d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
606645658c9Smatthias.ringwald         return;
607645658c9Smatthias.ringwald     }
608645658c9Smatthias.ringwald     // alloc structure
6097b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
610d3a9df87Smatthias.ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
6112b360848Smatthias.ringwald     if (!channel){
6122b360848Smatthias.ringwald         // 0x0004 No resources available
6132b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
6142b360848Smatthias.ringwald         return;
6152b360848Smatthias.ringwald     }
616645658c9Smatthias.ringwald 
617645658c9Smatthias.ringwald     // fill in
618169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
619169f8b28Smatthias.ringwald     channel->psm = psm;
620169f8b28Smatthias.ringwald     channel->handle = handle;
621169f8b28Smatthias.ringwald     channel->connection = service->connection;
622f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
623b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
624b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
625fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6260a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
627761b0451Smatthias.ringwald     channel->packets_granted = 0;
628b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
629645658c9Smatthias.ringwald 
6301d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
631fa8c92f6Smatthias.ringwald     if (channel->local_mtu > l2cap_max_l2cap_mtu()) {
632fa8c92f6Smatthias.ringwald         channel->local_mtu = l2cap_max_l2cap_mtu();
6339775e25bSmatthias.ringwald     }
6349775e25bSmatthias.ringwald 
635645658c9Smatthias.ringwald     // set initial state
636e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
63773cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
638e405ae81Smatthias.ringwald 
639645658c9Smatthias.ringwald     // add to connections list
640169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
641645658c9Smatthias.ringwald 
642e405ae81Smatthias.ringwald     // emit incoming connection request
643e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
644e405ae81Smatthias.ringwald }
645645658c9Smatthias.ringwald 
646b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
647b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
648e405ae81Smatthias.ringwald     if (!channel) {
6497d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
650e405ae81Smatthias.ringwald         return;
651e405ae81Smatthias.ringwald     }
652e405ae81Smatthias.ringwald 
653552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
654e405ae81Smatthias.ringwald 
655552d92a1Smatthias.ringwald     // process
656552d92a1Smatthias.ringwald     l2cap_run();
657e405ae81Smatthias.ringwald }
658645658c9Smatthias.ringwald 
659b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
660b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
661e405ae81Smatthias.ringwald     if (!channel) {
6627d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
663e405ae81Smatthias.ringwald         return;
664e405ae81Smatthias.ringwald     }
665e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
666e7ff783cSmatthias.ringwald     channel->reason = reason;
667e7ff783cSmatthias.ringwald     l2cap_run();
668645658c9Smatthias.ringwald }
669645658c9Smatthias.ringwald 
6702784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
671b1988dceSmatthias.ringwald 
672b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
673b1988dceSmatthias.ringwald 
6742784b77dSmatthias.ringwald     // accept the other's configuration options
6753de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
6763de7c0caSmatthias.ringwald     uint16_t pos     = 8;
6773de7c0caSmatthias.ringwald     while (pos < end_pos){
6781dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
6791dc511deSmatthias.ringwald         uint8_t length = command[pos++];
6801dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
6811dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
6821dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
6837b5fbe1fSmatthias.ringwald             // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
6841dc511deSmatthias.ringwald         }
6851dc511deSmatthias.ringwald         pos += length;
6861dc511deSmatthias.ringwald     }
6872784b77dSmatthias.ringwald }
6882784b77dSmatthias.ringwald 
689fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
6907b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
69173cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
69273cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
693fa8473a4Smatthias.ringwald     return 1;
694fa8473a4Smatthias.ringwald }
695fa8473a4Smatthias.ringwald 
696fa8473a4Smatthias.ringwald 
69700d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
6981e6aba47Smatthias.ringwald 
69900d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
70000d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
70138e5900eSmatthias.ringwald     uint16_t result = 0;
7021e6aba47Smatthias.ringwald 
7037b5fbe1fSmatthias.ringwald     log_info("signaling handler code %u, state %u\n", code, channel->state);
704b35f641cSmatthias.ringwald 
7059a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7069a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7079a011532Smatthias.ringwald         switch (channel->state){
708fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7099a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7102b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7119a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7129a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7139a011532Smatthias.ringwald                 break;
7149a011532Smatthias.ringwald 
7159a011532Smatthias.ringwald             default:
7169a011532Smatthias.ringwald                 // ignore in other states
7179a011532Smatthias.ringwald                 break;
7189a011532Smatthias.ringwald         }
7199a011532Smatthias.ringwald         return;
7209a011532Smatthias.ringwald     }
7219a011532Smatthias.ringwald 
7221e6aba47Smatthias.ringwald     switch (channel->state) {
7231e6aba47Smatthias.ringwald 
7241e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7251e6aba47Smatthias.ringwald             switch (code){
7261e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
72700d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
72838e5900eSmatthias.ringwald                     switch (result) {
72938e5900eSmatthias.ringwald                         case 0:
730169f8b28Smatthias.ringwald                             // successful connection
73100d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
732fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
73373cf2b3dSmatthias.ringwald                             channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
73438e5900eSmatthias.ringwald                             break;
73538e5900eSmatthias.ringwald                         case 1:
73638e5900eSmatthias.ringwald                             // connection pending. get some coffee
73738e5900eSmatthias.ringwald                             break;
73838e5900eSmatthias.ringwald                         default:
739eb920dbeSmatthias.ringwald                             // channel closed
740eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
741eb920dbeSmatthias.ringwald 
742f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
74338e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
744eb920dbeSmatthias.ringwald 
745eb920dbeSmatthias.ringwald                             // drop link key if security block
746eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
747eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
748eb920dbeSmatthias.ringwald                             }
749eb920dbeSmatthias.ringwald 
750eb920dbeSmatthias.ringwald                             // discard channel
751eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
752d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
75338e5900eSmatthias.ringwald                             break;
7541e6aba47Smatthias.ringwald                     }
7551e6aba47Smatthias.ringwald                     break;
75638e5900eSmatthias.ringwald 
75738e5900eSmatthias.ringwald                 default:
7581e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
75938e5900eSmatthias.ringwald                     break;
7601e6aba47Smatthias.ringwald             }
7611e6aba47Smatthias.ringwald             break;
7621e6aba47Smatthias.ringwald 
763fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
764ae280e73Smatthias.ringwald             switch (code) {
765ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
76673cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ;
76773cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
768ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
769ae280e73Smatthias.ringwald                     break;
7701e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
77173cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP;
7725a67bd4aSmatthias.ringwald                     break;
7735a67bd4aSmatthias.ringwald                 default:
7745a67bd4aSmatthias.ringwald                     break;
7751e6aba47Smatthias.ringwald             }
776fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
777fa8473a4Smatthias.ringwald                 // for open:
7785a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
779fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
7806218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
781c8e4258aSmatthias.ringwald             }
782c8e4258aSmatthias.ringwald             break;
783f62db1e3Smatthias.ringwald 
784f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
785f62db1e3Smatthias.ringwald             switch (code) {
786f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
78727a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
78827a923d0Smatthias.ringwald                     break;
7895a67bd4aSmatthias.ringwald                 default:
7905a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7915a67bd4aSmatthias.ringwald                     break;
79227a923d0Smatthias.ringwald             }
79327a923d0Smatthias.ringwald             break;
79484836b65Smatthias.ringwald 
79584836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
79684836b65Smatthias.ringwald             // @TODO handle incoming requests
79784836b65Smatthias.ringwald             break;
79884836b65Smatthias.ringwald 
79984836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
80084836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
80184836b65Smatthias.ringwald             break;
80210642e45Smatthias.ringwald         default:
80310642e45Smatthias.ringwald             break;
80427a923d0Smatthias.ringwald     }
8057b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
80627a923d0Smatthias.ringwald }
80727a923d0Smatthias.ringwald 
80800d93d79Smatthias.ringwald 
80900d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
81000d93d79Smatthias.ringwald 
81100d93d79Smatthias.ringwald     // get code, signalind identifier and command len
81200d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
81300d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
81400d93d79Smatthias.ringwald 
81500d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
81600d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
81700d93d79Smatthias.ringwald         return;
81800d93d79Smatthias.ringwald     }
81900d93d79Smatthias.ringwald 
82000d93d79Smatthias.ringwald     // general commands without an assigned channel
82100d93d79Smatthias.ringwald     switch(code) {
82200d93d79Smatthias.ringwald 
82300d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
82400d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
82500d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
82600d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8272b83fb7dSmatthias.ringwald             return;
82800d93d79Smatthias.ringwald         }
82900d93d79Smatthias.ringwald 
8302b360848Smatthias.ringwald         case ECHO_REQUEST:
8312b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
8322b83fb7dSmatthias.ringwald             return;
83300d93d79Smatthias.ringwald 
83400d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
83500d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
8362b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
8372b83fb7dSmatthias.ringwald             return;
83800d93d79Smatthias.ringwald         }
83900d93d79Smatthias.ringwald 
84000d93d79Smatthias.ringwald         default:
84100d93d79Smatthias.ringwald             break;
84200d93d79Smatthias.ringwald     }
84300d93d79Smatthias.ringwald 
84400d93d79Smatthias.ringwald 
84500d93d79Smatthias.ringwald     // Get potential destination CID
84600d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
84700d93d79Smatthias.ringwald 
84800d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
84900d93d79Smatthias.ringwald     linked_item_t *it;
85000d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
85100d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
85200d93d79Smatthias.ringwald         if (channel->handle == handle) {
85300d93d79Smatthias.ringwald             if (code & 1) {
854b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
855b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
85600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8574e32727eSmatthias.ringwald                     break;
85800d93d79Smatthias.ringwald                 }
85900d93d79Smatthias.ringwald             } else {
860b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
86100d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
86200d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8634e32727eSmatthias.ringwald                     break;
86400d93d79Smatthias.ringwald                 }
86500d93d79Smatthias.ringwald             }
86600d93d79Smatthias.ringwald         }
86700d93d79Smatthias.ringwald     }
86800d93d79Smatthias.ringwald }
86900d93d79Smatthias.ringwald 
87000d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
87100d93d79Smatthias.ringwald 
87200d93d79Smatthias.ringwald     // Get Channel ID
87300d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
87400d93d79Smatthias.ringwald 
87500d93d79Smatthias.ringwald     // Signaling Packet?
87600d93d79Smatthias.ringwald     if (channel_id == 1) {
87700d93d79Smatthias.ringwald 
87800d93d79Smatthias.ringwald         // Get Connection
87900d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
88000d93d79Smatthias.ringwald 
88100d93d79Smatthias.ringwald         uint16_t command_offset = 8;
88200d93d79Smatthias.ringwald         while (command_offset < size) {
88300d93d79Smatthias.ringwald 
88400d93d79Smatthias.ringwald             // handle signaling commands
88500d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
88600d93d79Smatthias.ringwald 
88700d93d79Smatthias.ringwald             // increment command_offset
88800d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
88900d93d79Smatthias.ringwald         }
89064472d52Smatthias.ringwald 
89164472d52Smatthias.ringwald         l2cap_run();
89264472d52Smatthias.ringwald 
89300d93d79Smatthias.ringwald         return;
89400d93d79Smatthias.ringwald     }
89500d93d79Smatthias.ringwald 
89600d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
89700d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
89800d93d79Smatthias.ringwald     if (channel) {
89958de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
90000d93d79Smatthias.ringwald     }
90100d93d79Smatthias.ringwald }
90200d93d79Smatthias.ringwald 
9032718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9042718e2e7Smatthias.ringwald     switch (packet_type) {
9052718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9062718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9072718e2e7Smatthias.ringwald             break;
9082718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9092718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9102718e2e7Smatthias.ringwald             break;
9112718e2e7Smatthias.ringwald         default:
9122718e2e7Smatthias.ringwald             break;
9132718e2e7Smatthias.ringwald     }
9142718e2e7Smatthias.ringwald }
91500d93d79Smatthias.ringwald 
91615ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
91727a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
918f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
919f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
920f62db1e3Smatthias.ringwald     // discard channel
921f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
922d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
923c8e4258aSmatthias.ringwald }
9241e6aba47Smatthias.ringwald 
9259d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
926c52bf64dSmatthias.ringwald     linked_item_t *it;
9279d9bbc01Smatthias.ringwald 
9289d9bbc01Smatthias.ringwald     // close open channels
9299d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9309d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9319d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9329d9bbc01Smatthias.ringwald             return service;
9339d9bbc01Smatthias.ringwald         };
9349d9bbc01Smatthias.ringwald     }
9359d9bbc01Smatthias.ringwald     return NULL;
9369d9bbc01Smatthias.ringwald }
9379d9bbc01Smatthias.ringwald 
93836944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
939*4bb582b6Smatthias.ringwald     // check for alread registered psm
940*4bb582b6Smatthias.ringwald     // TODO: emit error event
9419d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
942277abc2cSmatthias.ringwald     if (service) {
943277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
944277abc2cSmatthias.ringwald         return;
945277abc2cSmatthias.ringwald     }
9469d9bbc01Smatthias.ringwald 
947*4bb582b6Smatthias.ringwald     // alloc structure
948*4bb582b6Smatthias.ringwald     // TODO: emit error event
949d3a9df87Smatthias.ringwald     service = btstack_memory_l2cap_service_get();
950277abc2cSmatthias.ringwald     if (!service) {
951277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
952277abc2cSmatthias.ringwald         return;
953277abc2cSmatthias.ringwald     }
9549d9bbc01Smatthias.ringwald 
9559d9bbc01Smatthias.ringwald     // fill in
9569d9bbc01Smatthias.ringwald     service->psm = psm;
9579d9bbc01Smatthias.ringwald     service->mtu = mtu;
9589d9bbc01Smatthias.ringwald     service->connection = connection;
959d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
9609d9bbc01Smatthias.ringwald 
9619d9bbc01Smatthias.ringwald     // add to services list
9629d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
9639d9bbc01Smatthias.ringwald }
9649d9bbc01Smatthias.ringwald 
96536944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
9669d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
967037d6e48Smatthias.ringwald     if (!service) return;
9689d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
969d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
9709d9bbc01Smatthias.ringwald }
9719d9bbc01Smatthias.ringwald 
9729d9bbc01Smatthias.ringwald //
97336944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
9749d9bbc01Smatthias.ringwald     linked_item_t *it;
9759d9bbc01Smatthias.ringwald 
9763a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
977c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
978c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
979c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
980c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
981cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
982c52bf64dSmatthias.ringwald         }
983c52bf64dSmatthias.ringwald     }
9849d9bbc01Smatthias.ringwald 
985645658c9Smatthias.ringwald     // unregister services
98669025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
98769025de8Smatthias.ringwald     while (it->next) {
98869025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
989645658c9Smatthias.ringwald         if (service->connection == connection){
99069025de8Smatthias.ringwald             it->next = it->next->next;
991d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
9928149d2c7Smatthias.ringwald         } else {
9938149d2c7Smatthias.ringwald             it = it->next;
994645658c9Smatthias.ringwald         }
995645658c9Smatthias.ringwald     }
996cb8eb7e6Smatthias.ringwald 
997cb8eb7e6Smatthias.ringwald     // process
998cb8eb7e6Smatthias.ringwald     l2cap_run();
999c52bf64dSmatthias.ringwald }
1000c52bf64dSmatthias.ringwald