xref: /btstack/src/l2cap.c (revision ae280e7320c91fb5b97eb4d6f04f07f064d41e94)
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;
74808a48abSmatthias.ringwald static int new_credits_blocked = 0;
751e6aba47Smatthias.ringwald 
761e6aba47Smatthias.ringwald void l2cap_init(){
772784b77dSmatthias.ringwald     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
7811c41d51Smatthias.ringwald     acl_buffer = malloc( HCI_ACL_3DH5_SIZE);
79fcadd0caSmatthias.ringwald 
80808a48abSmatthias.ringwald     new_credits_blocked = 0;
81808a48abSmatthias.ringwald 
82fcadd0caSmatthias.ringwald     //
832718e2e7Smatthias.ringwald     // register callback with HCI
84fcadd0caSmatthias.ringwald     //
852718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
86fcadd0caSmatthias.ringwald }
87fcadd0caSmatthias.ringwald 
88fcadd0caSmatthias.ringwald 
89fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
9036944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
91fcadd0caSmatthias.ringwald }
9236944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
93b502e1b0Smatthias.ringwald     packet_handler = handler;
941e6aba47Smatthias.ringwald }
951e6aba47Smatthias.ringwald 
9658de5610Smatthias.ringwald //  notify client/protocol handler
9758de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
9858de5610Smatthias.ringwald     if (channel->packet_handler) {
9958de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
10058de5610Smatthias.ringwald     } else {
10136944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
10258de5610Smatthias.ringwald     }
10358de5610Smatthias.ringwald }
10458de5610Smatthias.ringwald 
10558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1069893b714Smatthias.ringwald     uint8_t event[21];
10758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
10858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
10958de5610Smatthias.ringwald     event[2] = status;
11058de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
11158de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
11258de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
11358de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
11458de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1154c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1164c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
11758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
11858de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
11958de5610Smatthias.ringwald }
12058de5610Smatthias.ringwald 
12158de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
12258de5610Smatthias.ringwald     uint8_t event[4];
12358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
12458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12558de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
12658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
12758de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
12858de5610Smatthias.ringwald }
12958de5610Smatthias.ringwald 
13058de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
13158de5610Smatthias.ringwald     uint8_t event[16];
13258de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
13358de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13458de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
13558de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
13658de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
13758de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
13858de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
13958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
14058de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1410af41d30Smatthias.ringwald }
142808a48abSmatthias.ringwald 
1436218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1446218e6f1Smatthias.ringwald     // track credits
1456218e6f1Smatthias.ringwald     channel->packets_granted += credits;
146808a48abSmatthias.ringwald     // log_dbg("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1476218e6f1Smatthias.ringwald 
1486218e6f1Smatthias.ringwald     uint8_t event[5];
1496218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1506218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1516218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1526218e6f1Smatthias.ringwald     event[4] = credits;
1536218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1546218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1556218e6f1Smatthias.ringwald }
1566218e6f1Smatthias.ringwald 
157808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
158808a48abSmatthias.ringwald     new_credits_blocked = blocked;
159808a48abSmatthias.ringwald }
160808a48abSmatthias.ringwald 
16140d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
162808a48abSmatthias.ringwald 
163808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
164808a48abSmatthias.ringwald 
1658d371091Smatthias.ringwald     linked_item_t *it;
1668d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1678d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1688d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1690e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1704c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1718d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1728d371091Smatthias.ringwald         }
1738d371091Smatthias.ringwald     }
1748d371091Smatthias.ringwald }
1758d371091Smatthias.ringwald 
176b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
177f62db1e3Smatthias.ringwald     linked_item_t *it;
178f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1798d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
180b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
181f62db1e3Smatthias.ringwald             return channel;
182f62db1e3Smatthias.ringwald         }
183f62db1e3Smatthias.ringwald     }
184f62db1e3Smatthias.ringwald     return NULL;
185f62db1e3Smatthias.ringwald }
186f62db1e3Smatthias.ringwald 
18796cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
18896cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
18996cbd662Smatthias.ringwald     if (channel) {
19096cbd662Smatthias.ringwald         return channel->remote_mtu;
19196cbd662Smatthias.ringwald     }
19296cbd662Smatthias.ringwald     return 0;
19396cbd662Smatthias.ringwald }
19496cbd662Smatthias.ringwald 
19558de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
196fe35119dSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet type %u\n", cmd);
19758de5610Smatthias.ringwald     va_list argptr;
19858de5610Smatthias.ringwald     va_start(argptr, identifier);
19958de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
20058de5610Smatthias.ringwald     va_end(argptr);
201808a48abSmatthias.ringwald     // log_dbg("l2cap_send_signaling_packet con %u!\n", handle);
20258de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
20358de5610Smatthias.ringwald }
20458de5610Smatthias.ringwald 
2056218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
2066218e6f1Smatthias.ringwald 
2076218e6f1Smatthias.ringwald     // check for free places on BT module
2088ea03fa5Smatthias.ringwald     if (!hci_number_free_acl_slots()) {
2098ea03fa5Smatthias.ringwald         log_dbg("l2cap_send_internal cid %u, BT module full <-----\n", local_cid);
210808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2118ea03fa5Smatthias.ringwald     }
2126218e6f1Smatthias.ringwald     int err = 0;
2136218e6f1Smatthias.ringwald 
21458de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
21558de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
21658de5610Smatthias.ringwald     if (channel) {
2176218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
2186218e6f1Smatthias.ringwald             --channel->packets_granted;
219808a48abSmatthias.ringwald             // log_dbg("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
220808a48abSmatthias.ringwald             //        local_cid, channel->handle, channel->packets_granted);
2216218e6f1Smatthias.ringwald         } else {
2226218e6f1Smatthias.ringwald             log_err("l2cap_send_internal cid %u, no credits!\n", local_cid);
2236218e6f1Smatthias.ringwald         }
2246218e6f1Smatthias.ringwald 
22558de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
22658de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
22758de5610Smatthias.ringwald         // 2 - ACL length
22858de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
22958de5610Smatthias.ringwald         // 4 - L2CAP packet length
23058de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
23158de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
23258de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
23358de5610Smatthias.ringwald         // 8 - data
23458de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
2356218e6f1Smatthias.ringwald 
23658de5610Smatthias.ringwald         // send
2376218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
23858de5610Smatthias.ringwald     }
23991b99603Smatthias.ringwald 
24091b99603Smatthias.ringwald     l2cap_hand_out_credits();
24191b99603Smatthias.ringwald 
2426218e6f1Smatthias.ringwald     return err;
24358de5610Smatthias.ringwald }
24458de5610Smatthias.ringwald 
2452cd0be45Smatthias.ringwald // process outstanding signaling tasks
2462cd0be45Smatthias.ringwald void l2cap_run(void){
247*ae280e73Smatthias.ringwald     uint8_t  config_options[4];
2482cd0be45Smatthias.ringwald     linked_item_t *it;
2492cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2502cd0be45Smatthias.ringwald 
2512cd0be45Smatthias.ringwald         // can send?
2522cd0be45Smatthias.ringwald 
2532cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
2542cd0be45Smatthias.ringwald         switch (channel->state){
2552cd0be45Smatthias.ringwald 
256e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
257e7ff783cSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, channel->reason, 0);
258e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
259e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
260e7ff783cSmatthias.ringwald                 free (channel);
261e7ff783cSmatthias.ringwald                 break;
262e7ff783cSmatthias.ringwald 
2636fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
2646fdcc387Smatthias.ringwald                 // success, start l2cap handshake
2656fdcc387Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
2666fdcc387Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid);
2676fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
2686fdcc387Smatthias.ringwald                 break;
2696fdcc387Smatthias.ringwald 
270*ae280e73Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
271*ae280e73Smatthias.ringwald                 // after connection response was received
272*ae280e73Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
273*ae280e73Smatthias.ringwald                 config_options[0] = 1; // MTU
274*ae280e73Smatthias.ringwald                 config_options[1] = 2; // len param
275*ae280e73Smatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
276*ae280e73Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
277*ae280e73Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
278*ae280e73Smatthias.ringwald                 break;
279*ae280e73Smatthias.ringwald 
280*ae280e73Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP:
281*ae280e73Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
282*ae280e73Smatthias.ringwald                 config_options[0] = 1; // MTU
283*ae280e73Smatthias.ringwald                 config_options[1] = 2; // len param
284*ae280e73Smatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
285*ae280e73Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
286*ae280e73Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
287*ae280e73Smatthias.ringwald                 break;
288*ae280e73Smatthias.ringwald 
289e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
290e7ff783cSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid);
291e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
292e7ff783cSmatthias.ringwald                 break;
293e7ff783cSmatthias.ringwald 
294e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
295dc8ceabbSmatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
2962cd0be45Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
2972cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
2982cd0be45Smatthias.ringwald                 break;
2992cd0be45Smatthias.ringwald             default:
3002cd0be45Smatthias.ringwald                 break;
3012cd0be45Smatthias.ringwald         }
3022cd0be45Smatthias.ringwald     }
3032cd0be45Smatthias.ringwald }
3042cd0be45Smatthias.ringwald 
3051e6aba47Smatthias.ringwald // open outgoing L2CAP channel
30615470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
30715470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
3081e6aba47Smatthias.ringwald 
3091e6aba47Smatthias.ringwald     // alloc structure
3101e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
3111e6aba47Smatthias.ringwald     // TODO: emit error event
3121e6aba47Smatthias.ringwald     if (!chan) return;
3131e6aba47Smatthias.ringwald 
3141d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
3151d279b20Smatthias.ringwald     if (mtu > hci_max_acl_data_packet_length()) {
3169775e25bSmatthias.ringwald         mtu = hci_max_acl_data_packet_length();
3179775e25bSmatthias.ringwald     }
3189775e25bSmatthias.ringwald 
3191e6aba47Smatthias.ringwald     // fill in
3201e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
3211e6aba47Smatthias.ringwald     chan->psm = psm;
3221e6aba47Smatthias.ringwald     chan->handle = 0;
3231e6aba47Smatthias.ringwald     chan->connection = connection;
3246b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
3252784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
32615470d27Smatthias.ringwald     chan->local_mtu = mtu;
3276218e6f1Smatthias.ringwald     chan->packets_granted = 0;
3286218e6f1Smatthias.ringwald 
3291e6aba47Smatthias.ringwald     // set initial state
3301e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
3311e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
3321e6aba47Smatthias.ringwald 
3331e6aba47Smatthias.ringwald     // add to connections list
3341e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
3351e6aba47Smatthias.ringwald 
3361e6aba47Smatthias.ringwald     // send connection request
3371e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
338d418e203Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0xcc18, 0, 0, 0, 1);
33943625864Smatthias.ringwald }
34043625864Smatthias.ringwald 
341b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
342b35f641cSmatthias.ringwald     // find channel for local_cid
343b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
344f62db1e3Smatthias.ringwald     if (channel) {
345e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
346f62db1e3Smatthias.ringwald     }
3472cd0be45Smatthias.ringwald     // process
3482cd0be45Smatthias.ringwald     l2cap_run();
34943625864Smatthias.ringwald }
3501e6aba47Smatthias.ringwald 
351afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
35215ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
35315ec09bbSmatthias.ringwald     while (it->next){
35415ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
355b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
356b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
357afde0c52Smatthias.ringwald                 // failure, forward error code
358afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
359afde0c52Smatthias.ringwald                 // discard channel
36015ec09bbSmatthias.ringwald                 it->next = it->next->next;
361afde0c52Smatthias.ringwald                 free (channel);
362afde0c52Smatthias.ringwald             }
36315ec09bbSmatthias.ringwald         } else {
36415ec09bbSmatthias.ringwald             it = it->next;
365afde0c52Smatthias.ringwald         }
366afde0c52Smatthias.ringwald     }
367afde0c52Smatthias.ringwald }
368afde0c52Smatthias.ringwald 
369afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
370afde0c52Smatthias.ringwald     linked_item_t *it;
371afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
372afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
373afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
374afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
375b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
3766fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
377afde0c52Smatthias.ringwald                 channel->handle = handle;
378b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
379afde0c52Smatthias.ringwald             }
380afde0c52Smatthias.ringwald         }
381afde0c52Smatthias.ringwald     }
3826fdcc387Smatthias.ringwald     // process
3836fdcc387Smatthias.ringwald     l2cap_run();
384afde0c52Smatthias.ringwald }
385b448a0e7Smatthias.ringwald 
386afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
387afde0c52Smatthias.ringwald 
388afde0c52Smatthias.ringwald     bd_addr_t address;
389afde0c52Smatthias.ringwald     hci_con_handle_t handle;
39036944dffSmatthias.ringwald     linked_item_t *it;
391afde0c52Smatthias.ringwald 
392afde0c52Smatthias.ringwald     switch(packet[0]){
393afde0c52Smatthias.ringwald 
394afde0c52Smatthias.ringwald         // handle connection complete events
395afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
396afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
397afde0c52Smatthias.ringwald             if (packet[2] == 0){
398afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
399afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
400afde0c52Smatthias.ringwald             } else {
401afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
402afde0c52Smatthias.ringwald             }
403afde0c52Smatthias.ringwald             break;
404afde0c52Smatthias.ringwald 
405afde0c52Smatthias.ringwald         // handle successful create connection cancel command
406afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
407afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
408afde0c52Smatthias.ringwald                 if (packet[5] == 0){
409afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
410afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
411afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
41203cfbabcSmatthias.ringwald                 }
4131e6aba47Smatthias.ringwald             }
414afde0c52Smatthias.ringwald             break;
41527a923d0Smatthias.ringwald 
4161e6aba47Smatthias.ringwald         // handle disconnection complete events
417afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
41827a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
419afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
42015ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
42115ec09bbSmatthias.ringwald             while (it->next){
42239ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
42327a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
42415ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
4252060cf14Smatthias.ringwald                     it->next = it->next->next;
42615ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
42715ec09bbSmatthias.ringwald                     free (channel);
42815ec09bbSmatthias.ringwald                 } else {
42915ec09bbSmatthias.ringwald                     it = it->next;
43027a923d0Smatthias.ringwald                 }
43127a923d0Smatthias.ringwald             }
432afde0c52Smatthias.ringwald             break;
433fcadd0caSmatthias.ringwald 
4346218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
4358d371091Smatthias.ringwald             l2cap_hand_out_credits();
4366218e6f1Smatthias.ringwald             break;
4376218e6f1Smatthias.ringwald 
438ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
439afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
44036944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
44180ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
442ee091cf1Smatthias.ringwald             l2cap_channel_t * channel;
443ee091cf1Smatthias.ringwald             int used = 0;
444ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
445ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
446ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
447ee091cf1Smatthias.ringwald                     used = 1;
448ee091cf1Smatthias.ringwald                 }
449ee091cf1Smatthias.ringwald             }
450ee091cf1Smatthias.ringwald             if (!used) {
4519edc8742Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
452ee091cf1Smatthias.ringwald             }
453afde0c52Smatthias.ringwald             break;
454ee091cf1Smatthias.ringwald 
455afde0c52Smatthias.ringwald         default:
456afde0c52Smatthias.ringwald             break;
457afde0c52Smatthias.ringwald     }
458afde0c52Smatthias.ringwald 
459afde0c52Smatthias.ringwald     // pass on
460b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
4611e6aba47Smatthias.ringwald }
4621e6aba47Smatthias.ringwald 
463afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
464e7ff783cSmatthias.ringwald     channel->sig_id = identifier;
465e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
466e7ff783cSmatthias.ringwald     l2cap_run();
46784836b65Smatthias.ringwald }
46884836b65Smatthias.ringwald 
469b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
470645658c9Smatthias.ringwald 
471fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
472645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
473645658c9Smatthias.ringwald     if (!service) {
474645658c9Smatthias.ringwald         // 0x0002 PSM not supported
475fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
476645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
477645658c9Smatthias.ringwald         return;
478645658c9Smatthias.ringwald     }
479645658c9Smatthias.ringwald 
480645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
481645658c9Smatthias.ringwald     if (!hci_connection) {
482fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
483645658c9Smatthias.ringwald         // TODO: emit error
484645658c9Smatthias.ringwald         return;
485645658c9Smatthias.ringwald     }
486645658c9Smatthias.ringwald     // alloc structure
487fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
488169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
489645658c9Smatthias.ringwald     // TODO: emit error event
490169f8b28Smatthias.ringwald     if (!channel) return;
491645658c9Smatthias.ringwald 
492645658c9Smatthias.ringwald     // fill in
493169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
494169f8b28Smatthias.ringwald     channel->psm = psm;
495169f8b28Smatthias.ringwald     channel->handle = handle;
496169f8b28Smatthias.ringwald     channel->connection = service->connection;
497f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
498b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
499b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
500fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
5010a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
502761b0451Smatthias.ringwald     channel->packets_granted = 0;
503645658c9Smatthias.ringwald 
5041d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
5051d279b20Smatthias.ringwald     if (channel->local_mtu > hci_max_acl_data_packet_length()) {
5069775e25bSmatthias.ringwald         channel->local_mtu = hci_max_acl_data_packet_length();
5079775e25bSmatthias.ringwald     }
5089775e25bSmatthias.ringwald 
509645658c9Smatthias.ringwald     // set initial state
510e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
511e405ae81Smatthias.ringwald 
512e405ae81Smatthias.ringwald     // temp. store req sig id
513e405ae81Smatthias.ringwald     channel->sig_id = sig_id;
514645658c9Smatthias.ringwald 
515645658c9Smatthias.ringwald     // add to connections list
516169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
517645658c9Smatthias.ringwald 
518e405ae81Smatthias.ringwald     // emit incoming connection request
519e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
520e405ae81Smatthias.ringwald }
521645658c9Smatthias.ringwald 
522b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
523b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
524e405ae81Smatthias.ringwald     if (!channel) {
525fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
526e405ae81Smatthias.ringwald         return;
527e405ae81Smatthias.ringwald     }
528e405ae81Smatthias.ringwald 
529e405ae81Smatthias.ringwald     // accept connection
530b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0);
531e405ae81Smatthias.ringwald 
532e405ae81Smatthias.ringwald     // set real sig and state and start config
533e405ae81Smatthias.ringwald     channel->sig_id = l2cap_next_sig_id();
534e405ae81Smatthias.ringwald     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
535fa2b2627Smatthias.ringwald     uint8_t config_options[4];
536fa2b2627Smatthias.ringwald     config_options[0] = 1; // MTU
537fa2b2627Smatthias.ringwald     config_options[1] = 2; // len param
538fa2b2627Smatthias.ringwald     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
539b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
540b35f641cSmatthias.ringwald 
541fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
542e405ae81Smatthias.ringwald }
543645658c9Smatthias.ringwald 
544b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
545b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
546e405ae81Smatthias.ringwald     if (!channel) {
547fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
548e405ae81Smatthias.ringwald         return;
549e405ae81Smatthias.ringwald     }
550e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
551e7ff783cSmatthias.ringwald     channel->reason = reason;
552e7ff783cSmatthias.ringwald     l2cap_run();
553645658c9Smatthias.ringwald }
554645658c9Smatthias.ringwald 
5552784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
5562784b77dSmatthias.ringwald     // accept the other's configuration options
5573de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
5583de7c0caSmatthias.ringwald     uint16_t pos     = 8;
5593de7c0caSmatthias.ringwald     while (pos < end_pos){
5601dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
5611dc511deSmatthias.ringwald         uint8_t length = command[pos++];
5621dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
5631dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
5641dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
565fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
5661dc511deSmatthias.ringwald         }
5671dc511deSmatthias.ringwald         pos += length;
5681dc511deSmatthias.ringwald     }
5692784b77dSmatthias.ringwald     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
5701dc511deSmatthias.ringwald     // send back OK
5711dc511deSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, 0, NULL);
5722784b77dSmatthias.ringwald }
5732784b77dSmatthias.ringwald 
57400d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
5751e6aba47Smatthias.ringwald 
57600d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
57700d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
57838e5900eSmatthias.ringwald     uint16_t result = 0;
5791e6aba47Smatthias.ringwald 
580fe35119dSmatthias.ringwald     // log_dbg("signaling handler code %u\n", code);
581b35f641cSmatthias.ringwald 
5821e6aba47Smatthias.ringwald     switch (channel->state) {
5831e6aba47Smatthias.ringwald 
5841e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
5851e6aba47Smatthias.ringwald             switch (code){
5861e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
58700d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
58838e5900eSmatthias.ringwald                     switch (result) {
58938e5900eSmatthias.ringwald                         case 0:
590169f8b28Smatthias.ringwald                             // successful connection
59100d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
592*ae280e73Smatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ;
593d418e203Smatthias.ringwald #if 0
594d418e203Smatthias.ringwald     channel->state = L2CAP_STATE_OPEN;
595d418e203Smatthias.ringwald     l2cap_emit_channel_opened(channel, 0);  // success
596d418e203Smatthias.ringwald     l2cap_emit_credits(channel, 1);
597d418e203Smatthias.ringwald #endif
59838e5900eSmatthias.ringwald                             break;
59938e5900eSmatthias.ringwald                         case 1:
60038e5900eSmatthias.ringwald                             // connection pending. get some coffee
60138e5900eSmatthias.ringwald                             break;
60238e5900eSmatthias.ringwald                         default:
603eb920dbeSmatthias.ringwald                             // channel closed
604eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
605eb920dbeSmatthias.ringwald 
606f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
60738e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
608eb920dbeSmatthias.ringwald 
609eb920dbeSmatthias.ringwald                             // drop link key if security block
610eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
611eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
612eb920dbeSmatthias.ringwald                             }
613eb920dbeSmatthias.ringwald 
614eb920dbeSmatthias.ringwald                             // discard channel
615eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
616eb920dbeSmatthias.ringwald                             free (channel);
61738e5900eSmatthias.ringwald                             break;
6181e6aba47Smatthias.ringwald                     }
6191e6aba47Smatthias.ringwald                     break;
62038e5900eSmatthias.ringwald 
62184836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
62284836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
62384836b65Smatthias.ringwald                     break;
62484836b65Smatthias.ringwald 
62538e5900eSmatthias.ringwald                 default:
6261e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
62738e5900eSmatthias.ringwald                     break;
6281e6aba47Smatthias.ringwald             }
6291e6aba47Smatthias.ringwald             break;
6301e6aba47Smatthias.ringwald 
631*ae280e73Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
632*ae280e73Smatthias.ringwald             switch (code) {
633*ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
634*ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
635*ae280e73Smatthias.ringwald                     channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP;
636*ae280e73Smatthias.ringwald                     break;
637*ae280e73Smatthias.ringwald                 case DISCONNECTION_REQUEST:
638*ae280e73Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
639*ae280e73Smatthias.ringwald                     break;
640*ae280e73Smatthias.ringwald                 default:
641*ae280e73Smatthias.ringwald                     //@TODO: implement other signaling packets
642*ae280e73Smatthias.ringwald                     break;
643*ae280e73Smatthias.ringwald             }
644*ae280e73Smatthias.ringwald             break;
645*ae280e73Smatthias.ringwald 
6465a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
6471e6aba47Smatthias.ringwald             switch (code) {
6481e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
6491e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
6501e6aba47Smatthias.ringwald                     break;
6515a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
6522784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
6535a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
6545a67bd4aSmatthias.ringwald                     break;
65584836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
65684836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
65784836b65Smatthias.ringwald                     break;
6585a67bd4aSmatthias.ringwald                 default:
6595a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6605a67bd4aSmatthias.ringwald                     break;
6611e6aba47Smatthias.ringwald             }
6621e6aba47Smatthias.ringwald             break;
6631e6aba47Smatthias.ringwald 
6641e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
6651e6aba47Smatthias.ringwald             switch (code) {
6661e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
6672784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
6681e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
66903cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
6706218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
671c8e4258aSmatthias.ringwald                     break;
67284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
67384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
67484836b65Smatthias.ringwald                     break;
6755a67bd4aSmatthias.ringwald                 default:
6765a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6775a67bd4aSmatthias.ringwald                     break;
6785a67bd4aSmatthias.ringwald             }
6795a67bd4aSmatthias.ringwald             break;
6805a67bd4aSmatthias.ringwald 
6815a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
6825a67bd4aSmatthias.ringwald             switch (code) {
6835a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
6845a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
6855a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
6866218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
6875a67bd4aSmatthias.ringwald                     break;
68884836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
68984836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
69084836b65Smatthias.ringwald                     break;
6915a67bd4aSmatthias.ringwald                 default:
6925a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6935a67bd4aSmatthias.ringwald                     break;
694c8e4258aSmatthias.ringwald             }
695c8e4258aSmatthias.ringwald             break;
696f62db1e3Smatthias.ringwald 
697f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
698f62db1e3Smatthias.ringwald             switch (code) {
699f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
70027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
70127a923d0Smatthias.ringwald                     break;
70284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
70384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
70484836b65Smatthias.ringwald                     break;
7055a67bd4aSmatthias.ringwald                 default:
7065a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7075a67bd4aSmatthias.ringwald                     break;
70827a923d0Smatthias.ringwald             }
70927a923d0Smatthias.ringwald             break;
71084836b65Smatthias.ringwald 
71184836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
71284836b65Smatthias.ringwald             // @TODO handle incoming requests
71384836b65Smatthias.ringwald             break;
71484836b65Smatthias.ringwald 
71584836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
71684836b65Smatthias.ringwald             switch (code) {
71784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
71884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
71984836b65Smatthias.ringwald                     break;
7205a67bd4aSmatthias.ringwald                 default:
72184836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
72284836b65Smatthias.ringwald                     break;
72384836b65Smatthias.ringwald             }
7245a67bd4aSmatthias.ringwald             break;
72510642e45Smatthias.ringwald         default:
72610642e45Smatthias.ringwald             break;
72727a923d0Smatthias.ringwald     }
728fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
72927a923d0Smatthias.ringwald }
73027a923d0Smatthias.ringwald 
73100d93d79Smatthias.ringwald 
73200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
73300d93d79Smatthias.ringwald 
73400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
73500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
73600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
73700d93d79Smatthias.ringwald     uint16_t len   = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
73800d93d79Smatthias.ringwald 
73900d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
74000d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
74100d93d79Smatthias.ringwald         return;
74200d93d79Smatthias.ringwald     }
74300d93d79Smatthias.ringwald 
74400d93d79Smatthias.ringwald     // general commands without an assigned channel
74500d93d79Smatthias.ringwald     switch(code) {
74600d93d79Smatthias.ringwald 
74700d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
74800d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
74900d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
75000d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
75100d93d79Smatthias.ringwald             break;
75200d93d79Smatthias.ringwald         }
75300d93d79Smatthias.ringwald 
75400d93d79Smatthias.ringwald         case ECHO_REQUEST: {
75500d93d79Smatthias.ringwald             // send back packet
75600d93d79Smatthias.ringwald             l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]);
75700d93d79Smatthias.ringwald             break;
75800d93d79Smatthias.ringwald         }
75900d93d79Smatthias.ringwald 
76000d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
76100d93d79Smatthias.ringwald             // we neither support connectionless L2CAP data nor support any flow control modes yet
76200d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
76300d93d79Smatthias.ringwald             if (infoType == 2) {
76400d93d79Smatthias.ringwald                 uint32_t features = 0;
76500d93d79Smatthias.ringwald                 // extended features request supported, however no features present
76600d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
76700d93d79Smatthias.ringwald             } else {
76800d93d79Smatthias.ringwald                 // all other types are not supported
76900d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
77000d93d79Smatthias.ringwald             }
77100d93d79Smatthias.ringwald             break;;
77200d93d79Smatthias.ringwald         }
77300d93d79Smatthias.ringwald 
77400d93d79Smatthias.ringwald         default:
77500d93d79Smatthias.ringwald             break;
77600d93d79Smatthias.ringwald     }
77700d93d79Smatthias.ringwald 
77800d93d79Smatthias.ringwald 
77900d93d79Smatthias.ringwald     // Get potential destination CID
78000d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
78100d93d79Smatthias.ringwald 
78200d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
78300d93d79Smatthias.ringwald     linked_item_t *it;
78400d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
78500d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
78600d93d79Smatthias.ringwald         if (channel->handle == handle) {
78700d93d79Smatthias.ringwald             if (code & 1) {
78800d93d79Smatthias.ringwald                 // match odd commands by previous signaling identifier
78900d93d79Smatthias.ringwald                 if (channel->sig_id == sig_id) {
79000d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
7914e32727eSmatthias.ringwald                     break;
79200d93d79Smatthias.ringwald                 }
79300d93d79Smatthias.ringwald             } else {
79400d93d79Smatthias.ringwald                 // match even commands by local channel id
79500d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
79600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
7974e32727eSmatthias.ringwald                     break;
79800d93d79Smatthias.ringwald                 }
79900d93d79Smatthias.ringwald             }
80000d93d79Smatthias.ringwald         }
80100d93d79Smatthias.ringwald     }
80200d93d79Smatthias.ringwald }
80300d93d79Smatthias.ringwald 
80400d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
80500d93d79Smatthias.ringwald 
80600d93d79Smatthias.ringwald     // Get Channel ID
80700d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
80800d93d79Smatthias.ringwald 
80900d93d79Smatthias.ringwald     // Signaling Packet?
81000d93d79Smatthias.ringwald     if (channel_id == 1) {
81100d93d79Smatthias.ringwald 
81200d93d79Smatthias.ringwald         // Get Connection
81300d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
81400d93d79Smatthias.ringwald 
81500d93d79Smatthias.ringwald         uint16_t command_offset = 8;
81600d93d79Smatthias.ringwald         while (command_offset < size) {
81700d93d79Smatthias.ringwald 
81800d93d79Smatthias.ringwald             // handle signaling commands
81900d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
82000d93d79Smatthias.ringwald 
82100d93d79Smatthias.ringwald             // increment command_offset
82200d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
82300d93d79Smatthias.ringwald         }
82400d93d79Smatthias.ringwald         return;
82500d93d79Smatthias.ringwald     }
82600d93d79Smatthias.ringwald 
82700d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
82800d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
82900d93d79Smatthias.ringwald     if (channel) {
83058de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
83100d93d79Smatthias.ringwald     }
83200d93d79Smatthias.ringwald }
83300d93d79Smatthias.ringwald 
8342718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
8352718e2e7Smatthias.ringwald     switch (packet_type) {
8362718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
8372718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
8382718e2e7Smatthias.ringwald             break;
8392718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
8402718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
8412718e2e7Smatthias.ringwald             break;
8422718e2e7Smatthias.ringwald         default:
8432718e2e7Smatthias.ringwald             break;
8442718e2e7Smatthias.ringwald     }
8452718e2e7Smatthias.ringwald }
84600d93d79Smatthias.ringwald 
84715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
84827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
849f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
850f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
851f62db1e3Smatthias.ringwald     // discard channel
852f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
853f62db1e3Smatthias.ringwald     free (channel);
854c8e4258aSmatthias.ringwald }
8551e6aba47Smatthias.ringwald 
8569d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
857c52bf64dSmatthias.ringwald     linked_item_t *it;
8589d9bbc01Smatthias.ringwald 
8599d9bbc01Smatthias.ringwald     // close open channels
8609d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
8619d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
8629d9bbc01Smatthias.ringwald         if ( service->psm == psm){
8639d9bbc01Smatthias.ringwald             return service;
8649d9bbc01Smatthias.ringwald         };
8659d9bbc01Smatthias.ringwald     }
8669d9bbc01Smatthias.ringwald     return NULL;
8679d9bbc01Smatthias.ringwald }
8689d9bbc01Smatthias.ringwald 
86936944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
8709d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
8719d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
8729d9bbc01Smatthias.ringwald     if (service) return;
8739d9bbc01Smatthias.ringwald 
8749d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
8759d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
8769d9bbc01Smatthias.ringwald     if (!service) return;
8779d9bbc01Smatthias.ringwald 
8789d9bbc01Smatthias.ringwald     // fill in
8799d9bbc01Smatthias.ringwald     service->psm = psm;
8809d9bbc01Smatthias.ringwald     service->mtu = mtu;
8819d9bbc01Smatthias.ringwald     service->connection = connection;
882d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
8839d9bbc01Smatthias.ringwald 
8849d9bbc01Smatthias.ringwald     // add to services list
8859d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
8869d9bbc01Smatthias.ringwald }
8879d9bbc01Smatthias.ringwald 
88836944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
8899d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
890037d6e48Smatthias.ringwald     if (!service) return;
8919d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
8929d9bbc01Smatthias.ringwald     free(service);
8939d9bbc01Smatthias.ringwald }
8949d9bbc01Smatthias.ringwald 
8959d9bbc01Smatthias.ringwald //
89636944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
8979d9bbc01Smatthias.ringwald     linked_item_t *it;
8989d9bbc01Smatthias.ringwald 
8993a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
900c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
901c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
902c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
903c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
904cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
905c52bf64dSmatthias.ringwald         }
906c52bf64dSmatthias.ringwald     }
9079d9bbc01Smatthias.ringwald 
908645658c9Smatthias.ringwald     // unregister services
90969025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
91069025de8Smatthias.ringwald     while (it->next) {
91169025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
912645658c9Smatthias.ringwald         if (service->connection == connection){
91369025de8Smatthias.ringwald             it->next = it->next->next;
9148149d2c7Smatthias.ringwald             free(service);
9158149d2c7Smatthias.ringwald         } else {
9168149d2c7Smatthias.ringwald             it = it->next;
917645658c9Smatthias.ringwald         }
918645658c9Smatthias.ringwald     }
919cb8eb7e6Smatthias.ringwald 
920cb8eb7e6Smatthias.ringwald     // process
921cb8eb7e6Smatthias.ringwald     l2cap_run();
922c52bf64dSmatthias.ringwald }
923c52bf64dSmatthias.ringwald