xref: /btstack/src/l2cap.c (revision 4e32727e89852565d50aa2b9924c1bcc32c803cb)
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 
691e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
701e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
719d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
721e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
7336944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
741e6aba47Smatthias.ringwald 
751e6aba47Smatthias.ringwald void l2cap_init(){
762784b77dSmatthias.ringwald     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
7711c41d51Smatthias.ringwald     acl_buffer = malloc( HCI_ACL_3DH5_SIZE);
78fcadd0caSmatthias.ringwald 
79fcadd0caSmatthias.ringwald     //
802718e2e7Smatthias.ringwald     // register callback with HCI
81fcadd0caSmatthias.ringwald     //
822718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
83fcadd0caSmatthias.ringwald }
84fcadd0caSmatthias.ringwald 
85fcadd0caSmatthias.ringwald 
86fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
8736944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
88fcadd0caSmatthias.ringwald }
8936944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
90b502e1b0Smatthias.ringwald     packet_handler = handler;
911e6aba47Smatthias.ringwald }
921e6aba47Smatthias.ringwald 
9358de5610Smatthias.ringwald //  notify client/protocol handler
9458de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
9558de5610Smatthias.ringwald     if (channel->packet_handler) {
9658de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
9758de5610Smatthias.ringwald     } else {
9836944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
9958de5610Smatthias.ringwald     }
10058de5610Smatthias.ringwald }
10158de5610Smatthias.ringwald 
10258de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
10358de5610Smatthias.ringwald     uint8_t event[17];
10458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
10558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
10658de5610Smatthias.ringwald     event[2] = status;
10758de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
10858de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
10958de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
11058de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
11158de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
11258de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
11358de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
11458de5610Smatthias.ringwald }
11558de5610Smatthias.ringwald 
11658de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
11758de5610Smatthias.ringwald     uint8_t event[4];
11858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
11958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12058de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
12158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
12258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
12358de5610Smatthias.ringwald }
12458de5610Smatthias.ringwald 
12558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
12658de5610Smatthias.ringwald     uint8_t event[16];
12758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
12858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12958de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
13058de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
13158de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
13258de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
13358de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
13458de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13558de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1360af41d30Smatthias.ringwald }
1370af41d30Smatthias.ringwald 
1386218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1396218e6f1Smatthias.ringwald     // track credits
1406218e6f1Smatthias.ringwald     channel->packets_granted += credits;
141a59fb47bSmatthias.ringwald     // log_dbg("l2cap_emit_credits for cid %u, credits now: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1426218e6f1Smatthias.ringwald 
1436218e6f1Smatthias.ringwald     uint8_t event[5];
1446218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1456218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1466218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1476218e6f1Smatthias.ringwald     event[4] = credits;
1486218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1496218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1506218e6f1Smatthias.ringwald }
1516218e6f1Smatthias.ringwald 
1528d371091Smatthias.ringwald void l2cap_hand_out_credits(){
1538d371091Smatthias.ringwald     linked_item_t *it;
1548d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1558d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1568d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1570e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1584c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1598d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1608d371091Smatthias.ringwald         }
1618d371091Smatthias.ringwald     }
1628d371091Smatthias.ringwald }
1638d371091Smatthias.ringwald 
164b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
165f62db1e3Smatthias.ringwald     linked_item_t *it;
166f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1678d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
168b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
169f62db1e3Smatthias.ringwald             return channel;
170f62db1e3Smatthias.ringwald         }
171f62db1e3Smatthias.ringwald     }
172f62db1e3Smatthias.ringwald     return NULL;
173f62db1e3Smatthias.ringwald }
174f62db1e3Smatthias.ringwald 
17596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
17696cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
17796cbd662Smatthias.ringwald     if (channel) {
17896cbd662Smatthias.ringwald         return channel->remote_mtu;
17996cbd662Smatthias.ringwald     }
18096cbd662Smatthias.ringwald     return 0;
18196cbd662Smatthias.ringwald }
18296cbd662Smatthias.ringwald 
18358de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
184fe35119dSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet type %u\n", cmd);
18558de5610Smatthias.ringwald     va_list argptr;
18658de5610Smatthias.ringwald     va_start(argptr, identifier);
18758de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
18858de5610Smatthias.ringwald     va_end(argptr);
18958de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
19058de5610Smatthias.ringwald }
19158de5610Smatthias.ringwald 
1926218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
1936218e6f1Smatthias.ringwald 
1946218e6f1Smatthias.ringwald     // check for free places on BT module
1958ea03fa5Smatthias.ringwald     if (!hci_number_free_acl_slots()) {
1968ea03fa5Smatthias.ringwald         log_dbg("l2cap_send_internal cid %u, BT module full <-----\n", local_cid);
1978ea03fa5Smatthias.ringwald         return -1;
1988ea03fa5Smatthias.ringwald     }
1996218e6f1Smatthias.ringwald     int err = 0;
2006218e6f1Smatthias.ringwald 
20158de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
20258de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
20358de5610Smatthias.ringwald     if (channel) {
2046218e6f1Smatthias.ringwald         // track sending
2056218e6f1Smatthias.ringwald         ++channel->packets_outgoing;
2066218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
2076218e6f1Smatthias.ringwald             --channel->packets_granted;
2088ea03fa5Smatthias.ringwald             // log_dbg("l2cap_send_internal cid %u, 1 credit used, credits left %u; outgoing count %u\n",
2098ea03fa5Smatthias.ringwald             //        local_cid, channel->packets_granted, channel->packets_outgoing);
2106218e6f1Smatthias.ringwald         } else {
2116218e6f1Smatthias.ringwald             log_err("l2cap_send_internal cid %u, no credits!\n", local_cid);
2126218e6f1Smatthias.ringwald         }
2136218e6f1Smatthias.ringwald 
21458de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
21558de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
21658de5610Smatthias.ringwald         // 2 - ACL length
21758de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
21858de5610Smatthias.ringwald         // 4 - L2CAP packet length
21958de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
22058de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
22158de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
22258de5610Smatthias.ringwald         // 8 - data
22358de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
2246218e6f1Smatthias.ringwald 
22558de5610Smatthias.ringwald         // send
2266218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
22758de5610Smatthias.ringwald     }
22891b99603Smatthias.ringwald 
22991b99603Smatthias.ringwald     l2cap_hand_out_credits();
23091b99603Smatthias.ringwald 
2316218e6f1Smatthias.ringwald     return err;
23258de5610Smatthias.ringwald }
23358de5610Smatthias.ringwald 
2341e6aba47Smatthias.ringwald // open outgoing L2CAP channel
23515470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
23615470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
2371e6aba47Smatthias.ringwald 
2381e6aba47Smatthias.ringwald     // alloc structure
2391e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
2401e6aba47Smatthias.ringwald     // TODO: emit error event
2411e6aba47Smatthias.ringwald     if (!chan) return;
2421e6aba47Smatthias.ringwald 
2439775e25bSmatthias.ringwald     // validate mtu
2449775e25bSmatthias.ringwald     if (hci_max_acl_data_packet_length() < mtu) {
2459775e25bSmatthias.ringwald         mtu = hci_max_acl_data_packet_length();
2469775e25bSmatthias.ringwald     }
2479775e25bSmatthias.ringwald 
2481e6aba47Smatthias.ringwald     // fill in
2491e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
2501e6aba47Smatthias.ringwald     chan->psm = psm;
2511e6aba47Smatthias.ringwald     chan->handle = 0;
2521e6aba47Smatthias.ringwald     chan->connection = connection;
2536b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
2542784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
25515470d27Smatthias.ringwald     chan->local_mtu = mtu;
2561e6aba47Smatthias.ringwald 
2576218e6f1Smatthias.ringwald     // flow control
2586218e6f1Smatthias.ringwald     chan->packets_granted = 0;
2596218e6f1Smatthias.ringwald     chan->packets_outgoing = 0;
2606218e6f1Smatthias.ringwald 
2611e6aba47Smatthias.ringwald     // set initial state
2621e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
2631e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
2641e6aba47Smatthias.ringwald 
2651e6aba47Smatthias.ringwald     // add to connections list
2661e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
2671e6aba47Smatthias.ringwald 
2681e6aba47Smatthias.ringwald     // send connection request
2691e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
2707af1ca85Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0xcc18, 0, 0, 0, 0);
27143625864Smatthias.ringwald }
27243625864Smatthias.ringwald 
273b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
274b35f641cSmatthias.ringwald     // find channel for local_cid
275b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
276f62db1e3Smatthias.ringwald     if (channel) {
277f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
278b35f641cSmatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
279f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
280f62db1e3Smatthias.ringwald     }
28143625864Smatthias.ringwald }
2821e6aba47Smatthias.ringwald 
283afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
28415ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
28515ec09bbSmatthias.ringwald     while (it->next){
28615ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
287b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
288b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
289afde0c52Smatthias.ringwald                 // failure, forward error code
290afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
291afde0c52Smatthias.ringwald                 // discard channel
29215ec09bbSmatthias.ringwald                 it->next = it->next->next;
293afde0c52Smatthias.ringwald                 free (channel);
294afde0c52Smatthias.ringwald             }
29515ec09bbSmatthias.ringwald         } else {
29615ec09bbSmatthias.ringwald             it = it->next;
297afde0c52Smatthias.ringwald         }
298afde0c52Smatthias.ringwald     }
299afde0c52Smatthias.ringwald }
300afde0c52Smatthias.ringwald 
301afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
302afde0c52Smatthias.ringwald     linked_item_t *it;
303afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
304afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
305afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
306afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
307b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
308afde0c52Smatthias.ringwald                 channel->handle = handle;
309b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
310b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
311b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
312b35f641cSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid);
313afde0c52Smatthias.ringwald             }
314afde0c52Smatthias.ringwald         }
315afde0c52Smatthias.ringwald     }
316afde0c52Smatthias.ringwald }
317b448a0e7Smatthias.ringwald 
318afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
319afde0c52Smatthias.ringwald 
320afde0c52Smatthias.ringwald     bd_addr_t address;
321afde0c52Smatthias.ringwald     hci_con_handle_t handle;
32236944dffSmatthias.ringwald     linked_item_t *it;
3236218e6f1Smatthias.ringwald     int i;
324afde0c52Smatthias.ringwald 
325afde0c52Smatthias.ringwald     switch(packet[0]){
326afde0c52Smatthias.ringwald 
327afde0c52Smatthias.ringwald         // handle connection complete events
328afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
329afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
330afde0c52Smatthias.ringwald             if (packet[2] == 0){
331afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
332afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
333afde0c52Smatthias.ringwald             } else {
334afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
335afde0c52Smatthias.ringwald             }
336afde0c52Smatthias.ringwald             break;
337afde0c52Smatthias.ringwald 
338afde0c52Smatthias.ringwald         // handle successful create connection cancel command
339afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
340afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
341afde0c52Smatthias.ringwald                 if (packet[5] == 0){
342afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
343afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
344afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
34503cfbabcSmatthias.ringwald                 }
3461e6aba47Smatthias.ringwald             }
347afde0c52Smatthias.ringwald             break;
34827a923d0Smatthias.ringwald 
3491e6aba47Smatthias.ringwald         // handle disconnection complete events
350afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
35127a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
352afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
35315ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
35415ec09bbSmatthias.ringwald             while (it->next){
35539ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
35627a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
35715ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
3582060cf14Smatthias.ringwald                     it->next = it->next->next;
35915ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
36015ec09bbSmatthias.ringwald                     free (channel);
36115ec09bbSmatthias.ringwald                 } else {
36215ec09bbSmatthias.ringwald                     it = it->next;
36327a923d0Smatthias.ringwald                 }
36427a923d0Smatthias.ringwald             }
365afde0c52Smatthias.ringwald             break;
366fcadd0caSmatthias.ringwald 
3676218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
3686218e6f1Smatthias.ringwald             for (i=0; i<packet[2];i++){
3696218e6f1Smatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
3706218e6f1Smatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
3716218e6f1Smatthias.ringwald                 while (num_packets) {
3728d371091Smatthias.ringwald 
3738d371091Smatthias.ringwald                     // find channels with max nr of pending outgoing packets
3748d371091Smatthias.ringwald                     l2cap_channel_t * fullest_channel = NULL;
3758d371091Smatthias.ringwald                     int max_nr_pending = 0;
3766218e6f1Smatthias.ringwald                     for (it = (linked_item_t *) &l2cap_channels; it->next ; it = it->next){
3776218e6f1Smatthias.ringwald                         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
3788d371091Smatthias.ringwald                         if (channel->packets_outgoing > max_nr_pending){
3798d371091Smatthias.ringwald                             fullest_channel = channel;
3808d371091Smatthias.ringwald                             max_nr_pending  = channel->packets_outgoing;
3816218e6f1Smatthias.ringwald                         }
3826218e6f1Smatthias.ringwald                     }
3836218e6f1Smatthias.ringwald 
3848d371091Smatthias.ringwald                     // decrease packets
3858d371091Smatthias.ringwald                     if (fullest_channel) {
3868d371091Smatthias.ringwald                         fullest_channel->packets_outgoing--;
3878ea03fa5Smatthias.ringwald                         // log_dbg("hci_number_completed_packet (l2cap) for cid %u, outgoing count %u\n",
3888ea03fa5Smatthias.ringwald                         //        fullest_channel->local_cid, fullest_channel->packets_outgoing);
3896218e6f1Smatthias.ringwald                     } else {
3906218e6f1Smatthias.ringwald                         log_err("hci_number_completed_packet but no outgoing packet in records\n");
3916218e6f1Smatthias.ringwald                     }
3928d371091Smatthias.ringwald 
3938d371091Smatthias.ringwald                     // handled!
3948d371091Smatthias.ringwald                     num_packets--;
3956218e6f1Smatthias.ringwald                 }
3966218e6f1Smatthias.ringwald             }
3978d371091Smatthias.ringwald             l2cap_hand_out_credits();
3986218e6f1Smatthias.ringwald             break;
3996218e6f1Smatthias.ringwald 
400ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
401afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
40236944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
40380ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
404ee091cf1Smatthias.ringwald             l2cap_channel_t * channel;
405ee091cf1Smatthias.ringwald             int used = 0;
406ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
407ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
408ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
409ee091cf1Smatthias.ringwald                     used = 1;
410ee091cf1Smatthias.ringwald                 }
411ee091cf1Smatthias.ringwald             }
412ee091cf1Smatthias.ringwald             if (!used) {
4139edc8742Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
414ee091cf1Smatthias.ringwald             }
415afde0c52Smatthias.ringwald             break;
416ee091cf1Smatthias.ringwald 
417afde0c52Smatthias.ringwald         default:
418afde0c52Smatthias.ringwald             break;
419afde0c52Smatthias.ringwald     }
420afde0c52Smatthias.ringwald 
421afde0c52Smatthias.ringwald     // pass on
422b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
4231e6aba47Smatthias.ringwald }
4241e6aba47Smatthias.ringwald 
425afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
426f79ee244Smatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->local_cid, channel->remote_cid);
42784836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
42884836b65Smatthias.ringwald }
42984836b65Smatthias.ringwald 
430b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
431645658c9Smatthias.ringwald 
432fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
433645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
434645658c9Smatthias.ringwald     if (!service) {
435645658c9Smatthias.ringwald         // 0x0002 PSM not supported
436fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
437645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
438645658c9Smatthias.ringwald         return;
439645658c9Smatthias.ringwald     }
440645658c9Smatthias.ringwald 
441645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
442645658c9Smatthias.ringwald     if (!hci_connection) {
443fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
444645658c9Smatthias.ringwald         // TODO: emit error
445645658c9Smatthias.ringwald         return;
446645658c9Smatthias.ringwald     }
447645658c9Smatthias.ringwald     // alloc structure
448fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
449169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
450645658c9Smatthias.ringwald     // TODO: emit error event
451169f8b28Smatthias.ringwald     if (!channel) return;
452645658c9Smatthias.ringwald 
453645658c9Smatthias.ringwald     // fill in
454169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
455169f8b28Smatthias.ringwald     channel->psm = psm;
456169f8b28Smatthias.ringwald     channel->handle = handle;
457169f8b28Smatthias.ringwald     channel->connection = service->connection;
458f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
459b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
460b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
461fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
4622784b77dSmatthias.ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
463645658c9Smatthias.ringwald 
4649775e25bSmatthias.ringwald     // validate mtu
4659775e25bSmatthias.ringwald     if (hci_max_acl_data_packet_length() < channel->local_mtu) {
4669775e25bSmatthias.ringwald         channel->local_mtu = hci_max_acl_data_packet_length();
4679775e25bSmatthias.ringwald     }
4689775e25bSmatthias.ringwald 
469645658c9Smatthias.ringwald     // set initial state
470e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
471e405ae81Smatthias.ringwald 
472e405ae81Smatthias.ringwald     // temp. store req sig id
473e405ae81Smatthias.ringwald     channel->sig_id = sig_id;
474645658c9Smatthias.ringwald 
475645658c9Smatthias.ringwald     // add to connections list
476169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
477645658c9Smatthias.ringwald 
478e405ae81Smatthias.ringwald     // emit incoming connection request
479e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
480e405ae81Smatthias.ringwald }
481645658c9Smatthias.ringwald 
482b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
483b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
484e405ae81Smatthias.ringwald     if (!channel) {
485fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
486e405ae81Smatthias.ringwald         return;
487e405ae81Smatthias.ringwald     }
488e405ae81Smatthias.ringwald 
489e405ae81Smatthias.ringwald     // accept connection
490b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0);
491e405ae81Smatthias.ringwald 
492e405ae81Smatthias.ringwald     // set real sig and state and start config
493e405ae81Smatthias.ringwald     channel->sig_id = l2cap_next_sig_id();
494e405ae81Smatthias.ringwald     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
495fa2b2627Smatthias.ringwald     uint8_t config_options[4];
496fa2b2627Smatthias.ringwald     config_options[0] = 1; // MTU
497fa2b2627Smatthias.ringwald     config_options[1] = 2; // len param
498fa2b2627Smatthias.ringwald     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
499b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
500b35f641cSmatthias.ringwald 
501fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
502e405ae81Smatthias.ringwald }
503645658c9Smatthias.ringwald 
504b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
505b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
506e405ae81Smatthias.ringwald     if (!channel) {
507fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
508e405ae81Smatthias.ringwald         return;
509e405ae81Smatthias.ringwald     }
510e405ae81Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
511e405ae81Smatthias.ringwald 
512e405ae81Smatthias.ringwald     // discard channel
513e405ae81Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
514e405ae81Smatthias.ringwald     free (channel);
515645658c9Smatthias.ringwald }
516645658c9Smatthias.ringwald 
5172784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
5182784b77dSmatthias.ringwald     // accept the other's configuration options
5193de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
5203de7c0caSmatthias.ringwald     uint16_t pos     = 8;
5213de7c0caSmatthias.ringwald     while (pos < end_pos){
5221dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
5231dc511deSmatthias.ringwald         uint8_t length = command[pos++];
5241dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
5251dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
5261dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
527fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
5281dc511deSmatthias.ringwald         }
5291dc511deSmatthias.ringwald         pos += length;
5301dc511deSmatthias.ringwald     }
5312784b77dSmatthias.ringwald     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
5321dc511deSmatthias.ringwald     // send back received options
5331dc511deSmatthias.ringwald     // l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]);
5341dc511deSmatthias.ringwald     // send back OK
5351dc511deSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, 0, NULL);
5362784b77dSmatthias.ringwald }
5372784b77dSmatthias.ringwald 
53800d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
5391e6aba47Smatthias.ringwald 
54000d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
54100d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
54238e5900eSmatthias.ringwald     uint16_t result = 0;
543fa2b2627Smatthias.ringwald     uint8_t  config_options[4];
5441e6aba47Smatthias.ringwald 
545fe35119dSmatthias.ringwald     // log_dbg("signaling handler code %u\n", code);
546b35f641cSmatthias.ringwald 
5471e6aba47Smatthias.ringwald     switch (channel->state) {
5481e6aba47Smatthias.ringwald 
5491e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
5501e6aba47Smatthias.ringwald             switch (code){
5511e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
55200d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
55338e5900eSmatthias.ringwald                     switch (result) {
55438e5900eSmatthias.ringwald                         case 0:
555169f8b28Smatthias.ringwald                             // successful connection
55600d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
5571e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
558fa2b2627Smatthias.ringwald                             config_options[0] = 1; // MTU
559fa2b2627Smatthias.ringwald                             config_options[1] = 2; // len param
560fa2b2627Smatthias.ringwald                             bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
561b35f641cSmatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
5625a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
56338e5900eSmatthias.ringwald                             break;
56438e5900eSmatthias.ringwald                         case 1:
56538e5900eSmatthias.ringwald                             // connection pending. get some coffee
56638e5900eSmatthias.ringwald                             break;
56738e5900eSmatthias.ringwald                         default:
568eb920dbeSmatthias.ringwald                             // channel closed
569eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
570eb920dbeSmatthias.ringwald 
571f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
57238e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
573eb920dbeSmatthias.ringwald 
574eb920dbeSmatthias.ringwald                             // drop link key if security block
575eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
576eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
577eb920dbeSmatthias.ringwald                             }
578eb920dbeSmatthias.ringwald 
579eb920dbeSmatthias.ringwald                             // discard channel
580eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
581eb920dbeSmatthias.ringwald                             free (channel);
58238e5900eSmatthias.ringwald                             break;
5831e6aba47Smatthias.ringwald                     }
5841e6aba47Smatthias.ringwald                     break;
58538e5900eSmatthias.ringwald 
58684836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
58784836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
58884836b65Smatthias.ringwald                     break;
58984836b65Smatthias.ringwald 
59038e5900eSmatthias.ringwald                 default:
5911e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
59238e5900eSmatthias.ringwald                     break;
5931e6aba47Smatthias.ringwald             }
5941e6aba47Smatthias.ringwald             break;
5951e6aba47Smatthias.ringwald 
5965a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
5971e6aba47Smatthias.ringwald             switch (code) {
5981e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
5991e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
6001e6aba47Smatthias.ringwald                     break;
6015a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
6022784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
6035a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
6045a67bd4aSmatthias.ringwald                     break;
60584836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
60684836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
60784836b65Smatthias.ringwald                     break;
6085a67bd4aSmatthias.ringwald                 default:
6095a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6105a67bd4aSmatthias.ringwald                     break;
6111e6aba47Smatthias.ringwald             }
6121e6aba47Smatthias.ringwald             break;
6131e6aba47Smatthias.ringwald 
6141e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
6151e6aba47Smatthias.ringwald             switch (code) {
6161e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
6172784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
6181e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
61903cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
6206218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
621c8e4258aSmatthias.ringwald                     break;
62284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
62384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
62484836b65Smatthias.ringwald                     break;
6255a67bd4aSmatthias.ringwald                 default:
6265a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6275a67bd4aSmatthias.ringwald                     break;
6285a67bd4aSmatthias.ringwald             }
6295a67bd4aSmatthias.ringwald             break;
6305a67bd4aSmatthias.ringwald 
6315a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
6325a67bd4aSmatthias.ringwald             switch (code) {
6335a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
6345a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
6355a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
6366218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
6375a67bd4aSmatthias.ringwald                     break;
63884836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
63984836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
64084836b65Smatthias.ringwald                     break;
6415a67bd4aSmatthias.ringwald                 default:
6425a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6435a67bd4aSmatthias.ringwald                     break;
644c8e4258aSmatthias.ringwald             }
645c8e4258aSmatthias.ringwald             break;
646f62db1e3Smatthias.ringwald 
647f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
648f62db1e3Smatthias.ringwald             switch (code) {
649f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
65027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
65127a923d0Smatthias.ringwald                     break;
65284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
65384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
65484836b65Smatthias.ringwald                     break;
6555a67bd4aSmatthias.ringwald                 default:
6565a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6575a67bd4aSmatthias.ringwald                     break;
65827a923d0Smatthias.ringwald             }
65927a923d0Smatthias.ringwald             break;
66084836b65Smatthias.ringwald 
66184836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
66284836b65Smatthias.ringwald             // @TODO handle incoming requests
66384836b65Smatthias.ringwald             break;
66484836b65Smatthias.ringwald 
66584836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
66684836b65Smatthias.ringwald             switch (code) {
66784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
66884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
66984836b65Smatthias.ringwald                     break;
6705a67bd4aSmatthias.ringwald                 default:
67184836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
67284836b65Smatthias.ringwald                     break;
67384836b65Smatthias.ringwald             }
6745a67bd4aSmatthias.ringwald             break;
67510642e45Smatthias.ringwald         default:
67610642e45Smatthias.ringwald             break;
67727a923d0Smatthias.ringwald     }
678fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
67927a923d0Smatthias.ringwald }
68027a923d0Smatthias.ringwald 
68100d93d79Smatthias.ringwald 
68200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
68300d93d79Smatthias.ringwald 
68400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
68500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
68600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
68700d93d79Smatthias.ringwald     uint16_t len   = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
68800d93d79Smatthias.ringwald 
68900d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
69000d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
69100d93d79Smatthias.ringwald         return;
69200d93d79Smatthias.ringwald     }
69300d93d79Smatthias.ringwald 
69400d93d79Smatthias.ringwald     // general commands without an assigned channel
69500d93d79Smatthias.ringwald     switch(code) {
69600d93d79Smatthias.ringwald 
69700d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
69800d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
69900d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
70000d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
70100d93d79Smatthias.ringwald             break;
70200d93d79Smatthias.ringwald         }
70300d93d79Smatthias.ringwald 
70400d93d79Smatthias.ringwald         case ECHO_REQUEST: {
70500d93d79Smatthias.ringwald             // send back packet
70600d93d79Smatthias.ringwald             l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]);
70700d93d79Smatthias.ringwald             break;
70800d93d79Smatthias.ringwald         }
70900d93d79Smatthias.ringwald 
71000d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
71100d93d79Smatthias.ringwald             // we neither support connectionless L2CAP data nor support any flow control modes yet
71200d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
71300d93d79Smatthias.ringwald             if (infoType == 2) {
71400d93d79Smatthias.ringwald                 uint32_t features = 0;
71500d93d79Smatthias.ringwald                 // extended features request supported, however no features present
71600d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
71700d93d79Smatthias.ringwald             } else {
71800d93d79Smatthias.ringwald                 // all other types are not supported
71900d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
72000d93d79Smatthias.ringwald             }
72100d93d79Smatthias.ringwald             break;;
72200d93d79Smatthias.ringwald         }
72300d93d79Smatthias.ringwald 
72400d93d79Smatthias.ringwald         default:
72500d93d79Smatthias.ringwald             break;
72600d93d79Smatthias.ringwald     }
72700d93d79Smatthias.ringwald 
72800d93d79Smatthias.ringwald 
72900d93d79Smatthias.ringwald     // Get potential destination CID
73000d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
73100d93d79Smatthias.ringwald 
73200d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
73300d93d79Smatthias.ringwald     linked_item_t *it;
73400d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
73500d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
73600d93d79Smatthias.ringwald         if (channel->handle == handle) {
73700d93d79Smatthias.ringwald             if (code & 1) {
73800d93d79Smatthias.ringwald                 // match odd commands by previous signaling identifier
73900d93d79Smatthias.ringwald                 if (channel->sig_id == sig_id) {
74000d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
741*4e32727eSmatthias.ringwald                     break;
74200d93d79Smatthias.ringwald                 }
74300d93d79Smatthias.ringwald             } else {
74400d93d79Smatthias.ringwald                 // match even commands by local channel id
74500d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
74600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
747*4e32727eSmatthias.ringwald                     break;
74800d93d79Smatthias.ringwald                 }
74900d93d79Smatthias.ringwald             }
75000d93d79Smatthias.ringwald         }
75100d93d79Smatthias.ringwald     }
75200d93d79Smatthias.ringwald }
75300d93d79Smatthias.ringwald 
75400d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
75500d93d79Smatthias.ringwald 
75600d93d79Smatthias.ringwald     // Get Channel ID
75700d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
75800d93d79Smatthias.ringwald 
75900d93d79Smatthias.ringwald     // Signaling Packet?
76000d93d79Smatthias.ringwald     if (channel_id == 1) {
76100d93d79Smatthias.ringwald 
76200d93d79Smatthias.ringwald         // Get Connection
76300d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
76400d93d79Smatthias.ringwald 
76500d93d79Smatthias.ringwald         uint16_t command_offset = 8;
76600d93d79Smatthias.ringwald         while (command_offset < size) {
76700d93d79Smatthias.ringwald 
76800d93d79Smatthias.ringwald             // handle signaling commands
76900d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
77000d93d79Smatthias.ringwald 
77100d93d79Smatthias.ringwald             // increment command_offset
77200d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
77300d93d79Smatthias.ringwald         }
77400d93d79Smatthias.ringwald         return;
77500d93d79Smatthias.ringwald     }
77600d93d79Smatthias.ringwald 
77700d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
77800d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
77900d93d79Smatthias.ringwald     if (channel) {
78058de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
78100d93d79Smatthias.ringwald     }
78200d93d79Smatthias.ringwald }
78300d93d79Smatthias.ringwald 
7842718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
7852718e2e7Smatthias.ringwald     switch (packet_type) {
7862718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
7872718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
7882718e2e7Smatthias.ringwald             break;
7892718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
7902718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
7912718e2e7Smatthias.ringwald             break;
7922718e2e7Smatthias.ringwald         default:
7932718e2e7Smatthias.ringwald             break;
7942718e2e7Smatthias.ringwald     }
7952718e2e7Smatthias.ringwald }
79600d93d79Smatthias.ringwald 
79715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
79827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
799f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
800f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
801f62db1e3Smatthias.ringwald     // discard channel
802f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
803f62db1e3Smatthias.ringwald     free (channel);
804c8e4258aSmatthias.ringwald }
8051e6aba47Smatthias.ringwald 
8069d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
807c52bf64dSmatthias.ringwald     linked_item_t *it;
8089d9bbc01Smatthias.ringwald 
8099d9bbc01Smatthias.ringwald     // close open channels
8109d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
8119d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
8129d9bbc01Smatthias.ringwald         if ( service->psm == psm){
8139d9bbc01Smatthias.ringwald             return service;
8149d9bbc01Smatthias.ringwald         };
8159d9bbc01Smatthias.ringwald     }
8169d9bbc01Smatthias.ringwald     return NULL;
8179d9bbc01Smatthias.ringwald }
8189d9bbc01Smatthias.ringwald 
81936944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
8209d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
8219d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
8229d9bbc01Smatthias.ringwald     if (service) return;
8239d9bbc01Smatthias.ringwald 
8249d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
8259d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
8269d9bbc01Smatthias.ringwald     if (!service) return;
8279d9bbc01Smatthias.ringwald 
8289d9bbc01Smatthias.ringwald     // fill in
8299d9bbc01Smatthias.ringwald     service->psm = psm;
8309d9bbc01Smatthias.ringwald     service->mtu = mtu;
8319d9bbc01Smatthias.ringwald     service->connection = connection;
832d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
8339d9bbc01Smatthias.ringwald 
8349d9bbc01Smatthias.ringwald     // add to services list
8359d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
8369d9bbc01Smatthias.ringwald }
8379d9bbc01Smatthias.ringwald 
83836944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
8399d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
840037d6e48Smatthias.ringwald     if (!service) return;
8419d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
8429d9bbc01Smatthias.ringwald     free(service);
8439d9bbc01Smatthias.ringwald }
8449d9bbc01Smatthias.ringwald 
8459d9bbc01Smatthias.ringwald //
84636944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
8479d9bbc01Smatthias.ringwald     linked_item_t *it;
8489d9bbc01Smatthias.ringwald 
8499d9bbc01Smatthias.ringwald     // close open channels
850c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
851c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
852c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
853c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
854c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
855b35f641cSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
856c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
857c52bf64dSmatthias.ringwald         }
858c52bf64dSmatthias.ringwald     }
8599d9bbc01Smatthias.ringwald 
860645658c9Smatthias.ringwald     // unregister services
86169025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
86269025de8Smatthias.ringwald     while (it->next) {
86369025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
864645658c9Smatthias.ringwald         if (service->connection == connection){
86569025de8Smatthias.ringwald             it->next = it->next->next;
8668149d2c7Smatthias.ringwald             free(service);
8678149d2c7Smatthias.ringwald         } else {
8688149d2c7Smatthias.ringwald             it = it->next;
869645658c9Smatthias.ringwald         }
870645658c9Smatthias.ringwald     }
871c52bf64dSmatthias.ringwald }
872c52bf64dSmatthias.ringwald