xref: /btstack/src/l2cap.c (revision 39d59809063b981fdbc8ef00dbc7fe2658fb4bb5)
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"
4443625864Smatthias.ringwald 
4543625864Smatthias.ringwald #include <stdarg.h>
4643625864Smatthias.ringwald #include <string.h>
4743625864Smatthias.ringwald 
4843625864Smatthias.ringwald #include <stdio.h>
4943625864Smatthias.ringwald 
506f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets
516f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8
526f60b3f4Smatthias.ringwald 
532784b77dSmatthias.ringwald // minimum signaling MTU
542784b77dSmatthias.ringwald #define L2CAP_MINIMAL_MTU 48
551dc511deSmatthias.ringwald #define L2CAP_DEFAULT_MTU 672
562784b77dSmatthias.ringwald 
574c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
584c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
594c744e21Smatthias.ringwald 
6000d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6100d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6200d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6500d93d79Smatthias.ringwald 
6636944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
672718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
68fcadd0caSmatthias.ringwald 
692b83fb7dSmatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
702b83fb7dSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 10
712b83fb7dSmatthias.ringwald typedef struct l2cap_signaling_response {
722b83fb7dSmatthias.ringwald     hci_con_handle_t handle;
732b83fb7dSmatthias.ringwald     uint8_t  sig_id;
742b83fb7dSmatthias.ringwald     uint8_t  code;
752b83fb7dSmatthias.ringwald     uint16_t infoType;
762b83fb7dSmatthias.ringwald } l2cap_signaling_response_t;
772b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
782b83fb7dSmatthias.ringwald static int signaling_responses_pending;
792b83fb7dSmatthias.ringwald 
801e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
811e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
829d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
831e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
8436944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
85808a48abSmatthias.ringwald static int new_credits_blocked = 0;
861e6aba47Smatthias.ringwald 
871e6aba47Smatthias.ringwald void l2cap_init(){
882784b77dSmatthias.ringwald     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
8911c41d51Smatthias.ringwald     acl_buffer = malloc( HCI_ACL_3DH5_SIZE);
90fcadd0caSmatthias.ringwald 
91808a48abSmatthias.ringwald     new_credits_blocked = 0;
922b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
93808a48abSmatthias.ringwald 
94fcadd0caSmatthias.ringwald     //
952718e2e7Smatthias.ringwald     // register callback with HCI
96fcadd0caSmatthias.ringwald     //
972718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
98fcadd0caSmatthias.ringwald }
99fcadd0caSmatthias.ringwald 
100fcadd0caSmatthias.ringwald 
101fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
10236944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
103fcadd0caSmatthias.ringwald }
10436944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
105b502e1b0Smatthias.ringwald     packet_handler = handler;
1061e6aba47Smatthias.ringwald }
1071e6aba47Smatthias.ringwald 
10858de5610Smatthias.ringwald //  notify client/protocol handler
10958de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
11058de5610Smatthias.ringwald     if (channel->packet_handler) {
11158de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
11258de5610Smatthias.ringwald     } else {
11336944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
11458de5610Smatthias.ringwald     }
11558de5610Smatthias.ringwald }
11658de5610Smatthias.ringwald 
11758de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1189893b714Smatthias.ringwald     uint8_t event[21];
11958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
12058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12158de5610Smatthias.ringwald     event[2] = status;
12258de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
12358de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
12458de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
12558de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
12658de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1274c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1284c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
12958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13058de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13158de5610Smatthias.ringwald }
13258de5610Smatthias.ringwald 
13358de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
13458de5610Smatthias.ringwald     uint8_t event[4];
13558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
13658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13758de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
13858de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13958de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
14058de5610Smatthias.ringwald }
14158de5610Smatthias.ringwald 
14258de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
14358de5610Smatthias.ringwald     uint8_t event[16];
14458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
14558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14658de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
14758de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
14858de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
14958de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
15058de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
15158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1530af41d30Smatthias.ringwald }
154808a48abSmatthias.ringwald 
1556218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1566218e6f1Smatthias.ringwald     // track credits
1576218e6f1Smatthias.ringwald     channel->packets_granted += credits;
158808a48abSmatthias.ringwald     // log_dbg("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1596218e6f1Smatthias.ringwald 
1606218e6f1Smatthias.ringwald     uint8_t event[5];
1616218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1626218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1636218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1646218e6f1Smatthias.ringwald     event[4] = credits;
1656218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1666218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1676218e6f1Smatthias.ringwald }
1686218e6f1Smatthias.ringwald 
169808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
170808a48abSmatthias.ringwald     new_credits_blocked = blocked;
171808a48abSmatthias.ringwald }
172808a48abSmatthias.ringwald 
17340d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
174808a48abSmatthias.ringwald 
175808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
176808a48abSmatthias.ringwald 
1778d371091Smatthias.ringwald     linked_item_t *it;
1788d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1798d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1808d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1810e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1824c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1838d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1848d371091Smatthias.ringwald         }
1858d371091Smatthias.ringwald     }
1868d371091Smatthias.ringwald }
1878d371091Smatthias.ringwald 
188b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
189f62db1e3Smatthias.ringwald     linked_item_t *it;
190f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1918d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
192b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
193f62db1e3Smatthias.ringwald             return channel;
194f62db1e3Smatthias.ringwald         }
195f62db1e3Smatthias.ringwald     }
196f62db1e3Smatthias.ringwald     return NULL;
197f62db1e3Smatthias.ringwald }
198f62db1e3Smatthias.ringwald 
19996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
20096cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
20196cbd662Smatthias.ringwald     if (channel) {
20296cbd662Smatthias.ringwald         return channel->remote_mtu;
20396cbd662Smatthias.ringwald     }
20496cbd662Smatthias.ringwald     return 0;
20596cbd662Smatthias.ringwald }
20696cbd662Smatthias.ringwald 
20758de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
208fe35119dSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet type %u\n", cmd);
20958de5610Smatthias.ringwald     va_list argptr;
21058de5610Smatthias.ringwald     va_start(argptr, identifier);
21158de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
21258de5610Smatthias.ringwald     va_end(argptr);
213808a48abSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet con %u!\n", handle);
21458de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
21558de5610Smatthias.ringwald }
21658de5610Smatthias.ringwald 
2176218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
2186218e6f1Smatthias.ringwald 
2196218e6f1Smatthias.ringwald     // check for free places on BT module
2208ea03fa5Smatthias.ringwald     if (!hci_number_free_acl_slots()) {
2218ea03fa5Smatthias.ringwald         log_dbg("l2cap_send_internal cid %u, BT module full <-----\n", local_cid);
222808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2238ea03fa5Smatthias.ringwald     }
2246218e6f1Smatthias.ringwald     int err = 0;
2256218e6f1Smatthias.ringwald 
22658de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
22758de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
22858de5610Smatthias.ringwald     if (channel) {
2296218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
2306218e6f1Smatthias.ringwald             --channel->packets_granted;
231808a48abSmatthias.ringwald             // log_dbg("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
232808a48abSmatthias.ringwald             //        local_cid, channel->handle, channel->packets_granted);
2336218e6f1Smatthias.ringwald         } else {
2346218e6f1Smatthias.ringwald             log_err("l2cap_send_internal cid %u, no credits!\n", local_cid);
2356218e6f1Smatthias.ringwald         }
2366218e6f1Smatthias.ringwald 
23758de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
23858de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
23958de5610Smatthias.ringwald         // 2 - ACL length
24058de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
24158de5610Smatthias.ringwald         // 4 - L2CAP packet length
24258de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
24358de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
24458de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
24558de5610Smatthias.ringwald         // 8 - data
24658de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
2476218e6f1Smatthias.ringwald 
24858de5610Smatthias.ringwald         // send
2496218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
25058de5610Smatthias.ringwald     }
25191b99603Smatthias.ringwald 
25291b99603Smatthias.ringwald     l2cap_hand_out_credits();
25391b99603Smatthias.ringwald 
2546218e6f1Smatthias.ringwald     return err;
25558de5610Smatthias.ringwald }
25658de5610Smatthias.ringwald 
2572cd0be45Smatthias.ringwald // process outstanding signaling tasks
2582cd0be45Smatthias.ringwald void l2cap_run(void){
2592b83fb7dSmatthias.ringwald 
2602b83fb7dSmatthias.ringwald 
2612b83fb7dSmatthias.ringwald     // check pending signaling responses
2622b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
2632b83fb7dSmatthias.ringwald 
26402b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
2652b83fb7dSmatthias.ringwald 
2662b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
2672b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
2682b83fb7dSmatthias.ringwald         uint8_t infoType = signaling_responses[0].infoType;
2692b83fb7dSmatthias.ringwald 
2702b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
2712b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
2722b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
2732b83fb7dSmatthias.ringwald                 break;
2742b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
2752b83fb7dSmatthias.ringwald                 if (infoType == 2) {
2762b83fb7dSmatthias.ringwald                     uint32_t features = 0;
2772b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
2782b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
2792b83fb7dSmatthias.ringwald                 } else {
2802b83fb7dSmatthias.ringwald                     // all other types are not supported
2812b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
2822b83fb7dSmatthias.ringwald                 }
2832b83fb7dSmatthias.ringwald                 break;
2842b83fb7dSmatthias.ringwald             default:
2852b83fb7dSmatthias.ringwald                 // should not happen
2862b83fb7dSmatthias.ringwald                 break;
2872b83fb7dSmatthias.ringwald         }
2882b83fb7dSmatthias.ringwald 
2892b83fb7dSmatthias.ringwald         // remove first item
2902b83fb7dSmatthias.ringwald         signaling_responses_pending--;
2912b83fb7dSmatthias.ringwald         int i;
2922b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
2932b83fb7dSmatthias.ringwald             signaling_responses[i] = signaling_responses[i+1];
2942b83fb7dSmatthias.ringwald         }
2952b83fb7dSmatthias.ringwald     }
2962b83fb7dSmatthias.ringwald 
297ae280e73Smatthias.ringwald     uint8_t  config_options[4];
2982cd0be45Smatthias.ringwald     linked_item_t *it;
2992cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3002cd0be45Smatthias.ringwald 
30102b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
30202b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3032cd0be45Smatthias.ringwald 
3042cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
3052cd0be45Smatthias.ringwald         switch (channel->state){
3062cd0be45Smatthias.ringwald 
30702b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
30864472d52Smatthias.ringwald                 // send connection request - set state first
30964472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
31002b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
31102b22dc4Smatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, 0xcc18, 0, 0, 0, 1);
31202b22dc4Smatthias.ringwald                 break;
31302b22dc4Smatthias.ringwald 
314e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
315b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
316e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
317e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
318e7ff783cSmatthias.ringwald                 free (channel);
319e7ff783cSmatthias.ringwald                 break;
320e7ff783cSmatthias.ringwald 
321552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
322b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
323552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ;
324552d92a1Smatthias.ringwald                 break;
325552d92a1Smatthias.ringwald 
3266fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
3276fdcc387Smatthias.ringwald                 // success, start l2cap handshake
328b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
329b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
3306fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
3316fdcc387Smatthias.ringwald                 break;
3326fdcc387Smatthias.ringwald 
333ae280e73Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
334ae280e73Smatthias.ringwald                 // after connection response was received
335b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
336ae280e73Smatthias.ringwald                 config_options[0] = 1; // MTU
337ae280e73Smatthias.ringwald                 config_options[1] = 2; // len param
338ae280e73Smatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
339b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
340ae280e73Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
341ae280e73Smatthias.ringwald                 break;
342ae280e73Smatthias.ringwald 
343b1988dceSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ:
344b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
345b1988dceSmatthias.ringwald                 config_options[0] = 1; // MTU
346b1988dceSmatthias.ringwald                 config_options[1] = 2; // len param
347b1988dceSmatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
348b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
349b1988dceSmatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
350b1988dceSmatthias.ringwald                 break;
351b1988dceSmatthias.ringwald 
352552d92a1Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP:
353b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
354552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
355552d92a1Smatthias.ringwald                 break;
356552d92a1Smatthias.ringwald 
357552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP:
358b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
359552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
360552d92a1Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);  // success
361552d92a1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
362552d92a1Smatthias.ringwald                 break;
363552d92a1Smatthias.ringwald 
364ae280e73Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP:
365b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
366552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ;
367552d92a1Smatthias.ringwald                 break;
368552d92a1Smatthias.ringwald 
369e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
370b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
371e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
372e7ff783cSmatthias.ringwald                 break;
373e7ff783cSmatthias.ringwald 
374e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
375b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
376b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
3772cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
3782cd0be45Smatthias.ringwald                 break;
3792cd0be45Smatthias.ringwald             default:
3802cd0be45Smatthias.ringwald                 break;
3812cd0be45Smatthias.ringwald         }
3822cd0be45Smatthias.ringwald     }
3832cd0be45Smatthias.ringwald }
3842cd0be45Smatthias.ringwald 
3851e6aba47Smatthias.ringwald // open outgoing L2CAP channel
38615470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
38715470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
3881e6aba47Smatthias.ringwald 
3891e6aba47Smatthias.ringwald     // alloc structure
3901e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
3911e6aba47Smatthias.ringwald     // TODO: emit error event
3921e6aba47Smatthias.ringwald     if (!chan) return;
3931e6aba47Smatthias.ringwald 
3941d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
3951d279b20Smatthias.ringwald     if (mtu > hci_max_acl_data_packet_length()) {
3969775e25bSmatthias.ringwald         mtu = hci_max_acl_data_packet_length();
3979775e25bSmatthias.ringwald     }
3989775e25bSmatthias.ringwald 
3991e6aba47Smatthias.ringwald     // fill in
4001e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4011e6aba47Smatthias.ringwald     chan->psm = psm;
4021e6aba47Smatthias.ringwald     chan->handle = 0;
4031e6aba47Smatthias.ringwald     chan->connection = connection;
4046b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4052784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
40615470d27Smatthias.ringwald     chan->local_mtu = mtu;
4076218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4086218e6f1Smatthias.ringwald 
4091e6aba47Smatthias.ringwald     // set initial state
41002b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
411b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
412b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
4131e6aba47Smatthias.ringwald 
4141e6aba47Smatthias.ringwald     // add to connections list
4151e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
4161e6aba47Smatthias.ringwald 
41702b22dc4Smatthias.ringwald     l2cap_run();
41843625864Smatthias.ringwald }
41943625864Smatthias.ringwald 
420b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
421b35f641cSmatthias.ringwald     // find channel for local_cid
422b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
423f62db1e3Smatthias.ringwald     if (channel) {
424e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
425f62db1e3Smatthias.ringwald     }
4262cd0be45Smatthias.ringwald     // process
4272cd0be45Smatthias.ringwald     l2cap_run();
42843625864Smatthias.ringwald }
4291e6aba47Smatthias.ringwald 
430afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
43115ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
43215ec09bbSmatthias.ringwald     while (it->next){
43315ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
434b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
43502b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
436afde0c52Smatthias.ringwald                 // failure, forward error code
437afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
438afde0c52Smatthias.ringwald                 // discard channel
43915ec09bbSmatthias.ringwald                 it->next = it->next->next;
440afde0c52Smatthias.ringwald                 free (channel);
441afde0c52Smatthias.ringwald             }
44215ec09bbSmatthias.ringwald         } else {
44315ec09bbSmatthias.ringwald             it = it->next;
444afde0c52Smatthias.ringwald         }
445afde0c52Smatthias.ringwald     }
446afde0c52Smatthias.ringwald }
447afde0c52Smatthias.ringwald 
448afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
449afde0c52Smatthias.ringwald     linked_item_t *it;
450afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
451afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
452afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
45302b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
454b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
4556fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
456afde0c52Smatthias.ringwald                 channel->handle = handle;
457b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
458afde0c52Smatthias.ringwald             }
459afde0c52Smatthias.ringwald         }
460afde0c52Smatthias.ringwald     }
4616fdcc387Smatthias.ringwald     // process
4626fdcc387Smatthias.ringwald     l2cap_run();
463afde0c52Smatthias.ringwald }
464b448a0e7Smatthias.ringwald 
465afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
466afde0c52Smatthias.ringwald 
467afde0c52Smatthias.ringwald     bd_addr_t address;
468afde0c52Smatthias.ringwald     hci_con_handle_t handle;
46936944dffSmatthias.ringwald     linked_item_t *it;
470afde0c52Smatthias.ringwald 
471afde0c52Smatthias.ringwald     switch(packet[0]){
472afde0c52Smatthias.ringwald 
473afde0c52Smatthias.ringwald         // handle connection complete events
474afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
475afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
476afde0c52Smatthias.ringwald             if (packet[2] == 0){
477afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
478afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
479afde0c52Smatthias.ringwald             } else {
480afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
481afde0c52Smatthias.ringwald             }
482afde0c52Smatthias.ringwald             break;
483afde0c52Smatthias.ringwald 
484afde0c52Smatthias.ringwald         // handle successful create connection cancel command
485afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
486afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
487afde0c52Smatthias.ringwald                 if (packet[5] == 0){
488afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
489afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
490afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
49103cfbabcSmatthias.ringwald                 }
4921e6aba47Smatthias.ringwald             }
493*39d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
494*39d59809Smatthias.ringwald             break;
495*39d59809Smatthias.ringwald 
496*39d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
497*39d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
498afde0c52Smatthias.ringwald             break;
49927a923d0Smatthias.ringwald 
5001e6aba47Smatthias.ringwald         // handle disconnection complete events
501afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
50227a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
503afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
50415ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
50515ec09bbSmatthias.ringwald             while (it->next){
50639ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
50727a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
50815ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
5092060cf14Smatthias.ringwald                     it->next = it->next->next;
51015ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
51115ec09bbSmatthias.ringwald                     free (channel);
51215ec09bbSmatthias.ringwald                 } else {
51315ec09bbSmatthias.ringwald                     it = it->next;
51427a923d0Smatthias.ringwald                 }
51527a923d0Smatthias.ringwald             }
516afde0c52Smatthias.ringwald             break;
517fcadd0caSmatthias.ringwald 
5186218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
51902b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
5208d371091Smatthias.ringwald             l2cap_hand_out_credits();
5216218e6f1Smatthias.ringwald             break;
5226218e6f1Smatthias.ringwald 
523ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
524afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
52536944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
52680ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
527ee091cf1Smatthias.ringwald             l2cap_channel_t * channel;
528ee091cf1Smatthias.ringwald             int used = 0;
529ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
530ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
531ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
532ee091cf1Smatthias.ringwald                     used = 1;
533ee091cf1Smatthias.ringwald                 }
534ee091cf1Smatthias.ringwald             }
53532ab9390Smatthias.ringwald             if (used) break;
53632ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
5379edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
538afde0c52Smatthias.ringwald             break;
539ee091cf1Smatthias.ringwald 
540afde0c52Smatthias.ringwald         default:
541afde0c52Smatthias.ringwald             break;
542afde0c52Smatthias.ringwald     }
543afde0c52Smatthias.ringwald 
544afde0c52Smatthias.ringwald     // pass on
545b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
5461e6aba47Smatthias.ringwald }
5471e6aba47Smatthias.ringwald 
548afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
549b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
550e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
551e7ff783cSmatthias.ringwald     l2cap_run();
55284836b65Smatthias.ringwald }
55384836b65Smatthias.ringwald 
554b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
555645658c9Smatthias.ringwald 
556fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
557645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
558645658c9Smatthias.ringwald     if (!service) {
559645658c9Smatthias.ringwald         // 0x0002 PSM not supported
560fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
561645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
562645658c9Smatthias.ringwald         return;
563645658c9Smatthias.ringwald     }
564645658c9Smatthias.ringwald 
565645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
566645658c9Smatthias.ringwald     if (!hci_connection) {
567fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
568645658c9Smatthias.ringwald         // TODO: emit error
569645658c9Smatthias.ringwald         return;
570645658c9Smatthias.ringwald     }
571645658c9Smatthias.ringwald     // alloc structure
572fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
573169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
574645658c9Smatthias.ringwald     // TODO: emit error event
575169f8b28Smatthias.ringwald     if (!channel) return;
576645658c9Smatthias.ringwald 
577645658c9Smatthias.ringwald     // fill in
578169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
579169f8b28Smatthias.ringwald     channel->psm = psm;
580169f8b28Smatthias.ringwald     channel->handle = handle;
581169f8b28Smatthias.ringwald     channel->connection = service->connection;
582f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
583b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
584b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
585fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
5860a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
587761b0451Smatthias.ringwald     channel->packets_granted = 0;
588b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
589645658c9Smatthias.ringwald 
5901d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
5911d279b20Smatthias.ringwald     if (channel->local_mtu > hci_max_acl_data_packet_length()) {
5929775e25bSmatthias.ringwald         channel->local_mtu = hci_max_acl_data_packet_length();
5939775e25bSmatthias.ringwald     }
5949775e25bSmatthias.ringwald 
595645658c9Smatthias.ringwald     // set initial state
596e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
597e405ae81Smatthias.ringwald 
598645658c9Smatthias.ringwald     // add to connections list
599169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
600645658c9Smatthias.ringwald 
601e405ae81Smatthias.ringwald     // emit incoming connection request
602e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
603e405ae81Smatthias.ringwald }
604645658c9Smatthias.ringwald 
605b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
606b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
607e405ae81Smatthias.ringwald     if (!channel) {
608fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
609e405ae81Smatthias.ringwald         return;
610e405ae81Smatthias.ringwald     }
611e405ae81Smatthias.ringwald 
612552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
613e405ae81Smatthias.ringwald 
614552d92a1Smatthias.ringwald     // process
615552d92a1Smatthias.ringwald     l2cap_run();
616e405ae81Smatthias.ringwald }
617645658c9Smatthias.ringwald 
618b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
619b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
620e405ae81Smatthias.ringwald     if (!channel) {
621fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
622e405ae81Smatthias.ringwald         return;
623e405ae81Smatthias.ringwald     }
624e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
625e7ff783cSmatthias.ringwald     channel->reason = reason;
626e7ff783cSmatthias.ringwald     l2cap_run();
627645658c9Smatthias.ringwald }
628645658c9Smatthias.ringwald 
6292784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
630b1988dceSmatthias.ringwald 
631b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
632b1988dceSmatthias.ringwald 
6332784b77dSmatthias.ringwald     // accept the other's configuration options
6343de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
6353de7c0caSmatthias.ringwald     uint16_t pos     = 8;
6363de7c0caSmatthias.ringwald     while (pos < end_pos){
6371dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
6381dc511deSmatthias.ringwald         uint8_t length = command[pos++];
6391dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
6401dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
6411dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
642fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
6431dc511deSmatthias.ringwald         }
6441dc511deSmatthias.ringwald         pos += length;
6451dc511deSmatthias.ringwald     }
6462784b77dSmatthias.ringwald }
6472784b77dSmatthias.ringwald 
64800d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
6491e6aba47Smatthias.ringwald 
65000d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
65100d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
65238e5900eSmatthias.ringwald     uint16_t result = 0;
6531e6aba47Smatthias.ringwald 
65464472d52Smatthias.ringwald     log_dbg("signaling handler code %u, state %u\n", code, channel->state);
655b35f641cSmatthias.ringwald 
6569a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
6579a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
6589a011532Smatthias.ringwald         switch (channel->state){
6599a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
6602b83fb7dSmatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP:
6619a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
6629a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ:
6639a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
6642b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ:
6652b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP:
6662b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP:
6679a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
6682b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
6699a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
6709a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
6719a011532Smatthias.ringwald                 break;
6729a011532Smatthias.ringwald 
6739a011532Smatthias.ringwald             default:
6749a011532Smatthias.ringwald                 // ignore in other states
6759a011532Smatthias.ringwald                 break;
6769a011532Smatthias.ringwald         }
6779a011532Smatthias.ringwald         return;
6789a011532Smatthias.ringwald     }
6799a011532Smatthias.ringwald 
6801e6aba47Smatthias.ringwald     switch (channel->state) {
6811e6aba47Smatthias.ringwald 
6821e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
6831e6aba47Smatthias.ringwald             switch (code){
6841e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
68500d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
68638e5900eSmatthias.ringwald                     switch (result) {
68738e5900eSmatthias.ringwald                         case 0:
688169f8b28Smatthias.ringwald                             // successful connection
68900d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
690ae280e73Smatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ;
691d418e203Smatthias.ringwald #if 0
692d418e203Smatthias.ringwald     channel->state = L2CAP_STATE_OPEN;
693d418e203Smatthias.ringwald     l2cap_emit_channel_opened(channel, 0);  // success
694d418e203Smatthias.ringwald     l2cap_emit_credits(channel, 1);
695d418e203Smatthias.ringwald #endif
69638e5900eSmatthias.ringwald                             break;
69738e5900eSmatthias.ringwald                         case 1:
69838e5900eSmatthias.ringwald                             // connection pending. get some coffee
69938e5900eSmatthias.ringwald                             break;
70038e5900eSmatthias.ringwald                         default:
701eb920dbeSmatthias.ringwald                             // channel closed
702eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
703eb920dbeSmatthias.ringwald 
704f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
70538e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
706eb920dbeSmatthias.ringwald 
707eb920dbeSmatthias.ringwald                             // drop link key if security block
708eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
709eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
710eb920dbeSmatthias.ringwald                             }
711eb920dbeSmatthias.ringwald 
712eb920dbeSmatthias.ringwald                             // discard channel
713eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
714eb920dbeSmatthias.ringwald                             free (channel);
71538e5900eSmatthias.ringwald                             break;
7161e6aba47Smatthias.ringwald                     }
7171e6aba47Smatthias.ringwald                     break;
71838e5900eSmatthias.ringwald 
71938e5900eSmatthias.ringwald                 default:
7201e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
72138e5900eSmatthias.ringwald                     break;
7221e6aba47Smatthias.ringwald             }
7231e6aba47Smatthias.ringwald             break;
7241e6aba47Smatthias.ringwald 
725ae280e73Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
726ae280e73Smatthias.ringwald             switch (code) {
727ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
728ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
729ae280e73Smatthias.ringwald                     channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP;
730ae280e73Smatthias.ringwald                     break;
731ae280e73Smatthias.ringwald                 default:
732ae280e73Smatthias.ringwald                     //@TODO: implement other signaling packets
733ae280e73Smatthias.ringwald                     break;
734ae280e73Smatthias.ringwald             }
735ae280e73Smatthias.ringwald             break;
736ae280e73Smatthias.ringwald 
7375a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
7381e6aba47Smatthias.ringwald             switch (code) {
7391e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
7401e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
7411e6aba47Smatthias.ringwald                     break;
7425a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
7432784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
744552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP;
7455a67bd4aSmatthias.ringwald                     break;
7465a67bd4aSmatthias.ringwald                 default:
7475a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7485a67bd4aSmatthias.ringwald                     break;
7491e6aba47Smatthias.ringwald             }
7501e6aba47Smatthias.ringwald             break;
7511e6aba47Smatthias.ringwald 
7521e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
7531e6aba47Smatthias.ringwald             switch (code) {
7541e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
7552784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
756552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP;
757c8e4258aSmatthias.ringwald                     break;
7585a67bd4aSmatthias.ringwald                 default:
7595a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7605a67bd4aSmatthias.ringwald                     break;
7615a67bd4aSmatthias.ringwald             }
7625a67bd4aSmatthias.ringwald             break;
7635a67bd4aSmatthias.ringwald 
7645a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
7655a67bd4aSmatthias.ringwald             switch (code) {
7665a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
7675a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
7685a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
7696218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
7705a67bd4aSmatthias.ringwald                     break;
7715a67bd4aSmatthias.ringwald                 default:
7725a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7735a67bd4aSmatthias.ringwald                     break;
774c8e4258aSmatthias.ringwald             }
775c8e4258aSmatthias.ringwald             break;
776f62db1e3Smatthias.ringwald 
777f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
778f62db1e3Smatthias.ringwald             switch (code) {
779f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
78027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
78127a923d0Smatthias.ringwald                     break;
7825a67bd4aSmatthias.ringwald                 default:
7835a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7845a67bd4aSmatthias.ringwald                     break;
78527a923d0Smatthias.ringwald             }
78627a923d0Smatthias.ringwald             break;
78784836b65Smatthias.ringwald 
78884836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
78984836b65Smatthias.ringwald             // @TODO handle incoming requests
79084836b65Smatthias.ringwald             break;
79184836b65Smatthias.ringwald 
79284836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
79384836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
79484836b65Smatthias.ringwald             break;
79510642e45Smatthias.ringwald         default:
79610642e45Smatthias.ringwald             break;
79727a923d0Smatthias.ringwald     }
798fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
79927a923d0Smatthias.ringwald }
80027a923d0Smatthias.ringwald 
80100d93d79Smatthias.ringwald 
80200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
80300d93d79Smatthias.ringwald 
80400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
80500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
80600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
80700d93d79Smatthias.ringwald 
80800d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
80900d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
81000d93d79Smatthias.ringwald         return;
81100d93d79Smatthias.ringwald     }
81200d93d79Smatthias.ringwald 
81300d93d79Smatthias.ringwald     // general commands without an assigned channel
81400d93d79Smatthias.ringwald     switch(code) {
81500d93d79Smatthias.ringwald 
81600d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
81700d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
81800d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
81900d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8202b83fb7dSmatthias.ringwald             return;
82100d93d79Smatthias.ringwald         }
82200d93d79Smatthias.ringwald 
82300d93d79Smatthias.ringwald         case ECHO_REQUEST: {
8242b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
8252b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
8262b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
8272b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
8282b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
8292b83fb7dSmatthias.ringwald             }
8302b83fb7dSmatthias.ringwald             l2cap_run();
8312b83fb7dSmatthias.ringwald             return;
83200d93d79Smatthias.ringwald         }
83300d93d79Smatthias.ringwald 
83400d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
8352b83fb7dSmatthias.ringwald             if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
83600d93d79Smatthias.ringwald                 // we neither support connectionless L2CAP data nor support any flow control modes yet
83700d93d79Smatthias.ringwald                 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
8382b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].handle = handle;
8392b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].code = code;
8402b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].sig_id = sig_id;
8412b83fb7dSmatthias.ringwald                 signaling_responses[signaling_responses_pending].infoType = infoType;
8422b83fb7dSmatthias.ringwald                 signaling_responses_pending++;
84300d93d79Smatthias.ringwald             }
8442b83fb7dSmatthias.ringwald             l2cap_run();
8452b83fb7dSmatthias.ringwald             return;
84600d93d79Smatthias.ringwald         }
84700d93d79Smatthias.ringwald 
84800d93d79Smatthias.ringwald         default:
84900d93d79Smatthias.ringwald             break;
85000d93d79Smatthias.ringwald     }
85100d93d79Smatthias.ringwald 
85200d93d79Smatthias.ringwald 
85300d93d79Smatthias.ringwald     // Get potential destination CID
85400d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
85500d93d79Smatthias.ringwald 
85600d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
85700d93d79Smatthias.ringwald     linked_item_t *it;
85800d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
85900d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
86000d93d79Smatthias.ringwald         if (channel->handle == handle) {
86100d93d79Smatthias.ringwald             if (code & 1) {
862b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
863b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
86400d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8654e32727eSmatthias.ringwald                     break;
86600d93d79Smatthias.ringwald                 }
86700d93d79Smatthias.ringwald             } else {
868b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
86900d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
87000d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8714e32727eSmatthias.ringwald                     break;
87200d93d79Smatthias.ringwald                 }
87300d93d79Smatthias.ringwald             }
87400d93d79Smatthias.ringwald         }
87500d93d79Smatthias.ringwald     }
87600d93d79Smatthias.ringwald }
87700d93d79Smatthias.ringwald 
87800d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
87900d93d79Smatthias.ringwald 
88000d93d79Smatthias.ringwald     // Get Channel ID
88100d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
88200d93d79Smatthias.ringwald 
88300d93d79Smatthias.ringwald     // Signaling Packet?
88400d93d79Smatthias.ringwald     if (channel_id == 1) {
88500d93d79Smatthias.ringwald 
88600d93d79Smatthias.ringwald         // Get Connection
88700d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
88800d93d79Smatthias.ringwald 
88900d93d79Smatthias.ringwald         uint16_t command_offset = 8;
89000d93d79Smatthias.ringwald         while (command_offset < size) {
89100d93d79Smatthias.ringwald 
89200d93d79Smatthias.ringwald             // handle signaling commands
89300d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
89400d93d79Smatthias.ringwald 
89500d93d79Smatthias.ringwald             // increment command_offset
89600d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
89700d93d79Smatthias.ringwald         }
89864472d52Smatthias.ringwald 
89964472d52Smatthias.ringwald         l2cap_run();
90064472d52Smatthias.ringwald 
90100d93d79Smatthias.ringwald         return;
90200d93d79Smatthias.ringwald     }
90300d93d79Smatthias.ringwald 
90400d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
90500d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
90600d93d79Smatthias.ringwald     if (channel) {
90758de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
90800d93d79Smatthias.ringwald     }
90900d93d79Smatthias.ringwald }
91000d93d79Smatthias.ringwald 
9112718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9122718e2e7Smatthias.ringwald     switch (packet_type) {
9132718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9142718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9152718e2e7Smatthias.ringwald             break;
9162718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9172718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9182718e2e7Smatthias.ringwald             break;
9192718e2e7Smatthias.ringwald         default:
9202718e2e7Smatthias.ringwald             break;
9212718e2e7Smatthias.ringwald     }
9222718e2e7Smatthias.ringwald }
92300d93d79Smatthias.ringwald 
92415ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
92527a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
926f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
927f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
928f62db1e3Smatthias.ringwald     // discard channel
929f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
930f62db1e3Smatthias.ringwald     free (channel);
931c8e4258aSmatthias.ringwald }
9321e6aba47Smatthias.ringwald 
9339d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
934c52bf64dSmatthias.ringwald     linked_item_t *it;
9359d9bbc01Smatthias.ringwald 
9369d9bbc01Smatthias.ringwald     // close open channels
9379d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9389d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9399d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9409d9bbc01Smatthias.ringwald             return service;
9419d9bbc01Smatthias.ringwald         };
9429d9bbc01Smatthias.ringwald     }
9439d9bbc01Smatthias.ringwald     return NULL;
9449d9bbc01Smatthias.ringwald }
9459d9bbc01Smatthias.ringwald 
94636944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
9479d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
9489d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
9499d9bbc01Smatthias.ringwald     if (service) return;
9509d9bbc01Smatthias.ringwald 
9519d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
9529d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
9539d9bbc01Smatthias.ringwald     if (!service) return;
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);
9699d9bbc01Smatthias.ringwald     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;
9918149d2c7Smatthias.ringwald             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