xref: /btstack/src/l2cap.c (revision b1988dce470e12aa64027fe220beabc02e642d57)
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){
247ae280e73Smatthias.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:
257*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_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 
263552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
264*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
265552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ;
266552d92a1Smatthias.ringwald                 break;
267552d92a1Smatthias.ringwald 
2686fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
2696fdcc387Smatthias.ringwald                 // success, start l2cap handshake
270*b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
271*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
2726fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
2736fdcc387Smatthias.ringwald                 break;
2746fdcc387Smatthias.ringwald 
275ae280e73Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
276ae280e73Smatthias.ringwald                 // after connection response was received
277*b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
278ae280e73Smatthias.ringwald                 config_options[0] = 1; // MTU
279ae280e73Smatthias.ringwald                 config_options[1] = 2; // len param
280ae280e73Smatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
281*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
282ae280e73Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
283ae280e73Smatthias.ringwald                 break;
284ae280e73Smatthias.ringwald 
285*b1988dceSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ:
286*b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
287*b1988dceSmatthias.ringwald                 config_options[0] = 1; // MTU
288*b1988dceSmatthias.ringwald                 config_options[1] = 2; // len param
289*b1988dceSmatthias.ringwald                 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
290*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
291*b1988dceSmatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
292*b1988dceSmatthias.ringwald                 break;
293*b1988dceSmatthias.ringwald 
294552d92a1Smatthias.ringwald             case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP:
295*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
296552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
297552d92a1Smatthias.ringwald                 break;
298552d92a1Smatthias.ringwald 
299552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP:
300*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
301552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
302552d92a1Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);  // success
303552d92a1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
304552d92a1Smatthias.ringwald                 break;
305552d92a1Smatthias.ringwald 
306ae280e73Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP:
307*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
308552d92a1Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ;
309552d92a1Smatthias.ringwald                 break;
310552d92a1Smatthias.ringwald 
311e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
312*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
313e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
314e7ff783cSmatthias.ringwald                 break;
315e7ff783cSmatthias.ringwald 
316e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
317*b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
318*b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
3192cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
3202cd0be45Smatthias.ringwald                 break;
3212cd0be45Smatthias.ringwald             default:
3222cd0be45Smatthias.ringwald                 break;
3232cd0be45Smatthias.ringwald         }
3242cd0be45Smatthias.ringwald     }
3252cd0be45Smatthias.ringwald }
3262cd0be45Smatthias.ringwald 
3271e6aba47Smatthias.ringwald // open outgoing L2CAP channel
32815470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
32915470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
3301e6aba47Smatthias.ringwald 
3311e6aba47Smatthias.ringwald     // alloc structure
3321e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
3331e6aba47Smatthias.ringwald     // TODO: emit error event
3341e6aba47Smatthias.ringwald     if (!chan) return;
3351e6aba47Smatthias.ringwald 
3361d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
3371d279b20Smatthias.ringwald     if (mtu > hci_max_acl_data_packet_length()) {
3389775e25bSmatthias.ringwald         mtu = hci_max_acl_data_packet_length();
3399775e25bSmatthias.ringwald     }
3409775e25bSmatthias.ringwald 
3411e6aba47Smatthias.ringwald     // fill in
3421e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
3431e6aba47Smatthias.ringwald     chan->psm = psm;
3441e6aba47Smatthias.ringwald     chan->handle = 0;
3451e6aba47Smatthias.ringwald     chan->connection = connection;
3466b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
3472784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
34815470d27Smatthias.ringwald     chan->local_mtu = mtu;
3496218e6f1Smatthias.ringwald     chan->packets_granted = 0;
3506218e6f1Smatthias.ringwald 
3511e6aba47Smatthias.ringwald     // set initial state
3521e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
353*b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
354*b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
3551e6aba47Smatthias.ringwald 
3561e6aba47Smatthias.ringwald     // add to connections list
3571e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
3581e6aba47Smatthias.ringwald 
3591e6aba47Smatthias.ringwald     // send connection request
3601e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
361d418e203Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0xcc18, 0, 0, 0, 1);
36243625864Smatthias.ringwald }
36343625864Smatthias.ringwald 
364b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
365b35f641cSmatthias.ringwald     // find channel for local_cid
366b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
367f62db1e3Smatthias.ringwald     if (channel) {
368e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
369f62db1e3Smatthias.ringwald     }
3702cd0be45Smatthias.ringwald     // process
3712cd0be45Smatthias.ringwald     l2cap_run();
37243625864Smatthias.ringwald }
3731e6aba47Smatthias.ringwald 
374afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
37515ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
37615ec09bbSmatthias.ringwald     while (it->next){
37715ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
378b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
379b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
380afde0c52Smatthias.ringwald                 // failure, forward error code
381afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
382afde0c52Smatthias.ringwald                 // discard channel
38315ec09bbSmatthias.ringwald                 it->next = it->next->next;
384afde0c52Smatthias.ringwald                 free (channel);
385afde0c52Smatthias.ringwald             }
38615ec09bbSmatthias.ringwald         } else {
38715ec09bbSmatthias.ringwald             it = it->next;
388afde0c52Smatthias.ringwald         }
389afde0c52Smatthias.ringwald     }
390afde0c52Smatthias.ringwald }
391afde0c52Smatthias.ringwald 
392afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
393afde0c52Smatthias.ringwald     linked_item_t *it;
394afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
395afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
396afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
397afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
398b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
3996fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
400afde0c52Smatthias.ringwald                 channel->handle = handle;
401b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
402afde0c52Smatthias.ringwald             }
403afde0c52Smatthias.ringwald         }
404afde0c52Smatthias.ringwald     }
4056fdcc387Smatthias.ringwald     // process
4066fdcc387Smatthias.ringwald     l2cap_run();
407afde0c52Smatthias.ringwald }
408b448a0e7Smatthias.ringwald 
409afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
410afde0c52Smatthias.ringwald 
411afde0c52Smatthias.ringwald     bd_addr_t address;
412afde0c52Smatthias.ringwald     hci_con_handle_t handle;
41336944dffSmatthias.ringwald     linked_item_t *it;
414afde0c52Smatthias.ringwald 
415afde0c52Smatthias.ringwald     switch(packet[0]){
416afde0c52Smatthias.ringwald 
417afde0c52Smatthias.ringwald         // handle connection complete events
418afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
419afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
420afde0c52Smatthias.ringwald             if (packet[2] == 0){
421afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
422afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
423afde0c52Smatthias.ringwald             } else {
424afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
425afde0c52Smatthias.ringwald             }
426afde0c52Smatthias.ringwald             break;
427afde0c52Smatthias.ringwald 
428afde0c52Smatthias.ringwald         // handle successful create connection cancel command
429afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
430afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
431afde0c52Smatthias.ringwald                 if (packet[5] == 0){
432afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
433afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
434afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
43503cfbabcSmatthias.ringwald                 }
4361e6aba47Smatthias.ringwald             }
437afde0c52Smatthias.ringwald             break;
43827a923d0Smatthias.ringwald 
4391e6aba47Smatthias.ringwald         // handle disconnection complete events
440afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
44127a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
442afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
44315ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
44415ec09bbSmatthias.ringwald             while (it->next){
44539ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
44627a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
44715ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
4482060cf14Smatthias.ringwald                     it->next = it->next->next;
44915ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
45015ec09bbSmatthias.ringwald                     free (channel);
45115ec09bbSmatthias.ringwald                 } else {
45215ec09bbSmatthias.ringwald                     it = it->next;
45327a923d0Smatthias.ringwald                 }
45427a923d0Smatthias.ringwald             }
455afde0c52Smatthias.ringwald             break;
456fcadd0caSmatthias.ringwald 
4576218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
4588d371091Smatthias.ringwald             l2cap_hand_out_credits();
4596218e6f1Smatthias.ringwald             break;
4606218e6f1Smatthias.ringwald 
461ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
462afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
46336944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
46480ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
465ee091cf1Smatthias.ringwald             l2cap_channel_t * channel;
466ee091cf1Smatthias.ringwald             int used = 0;
467ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
468ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
469ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
470ee091cf1Smatthias.ringwald                     used = 1;
471ee091cf1Smatthias.ringwald                 }
472ee091cf1Smatthias.ringwald             }
473ee091cf1Smatthias.ringwald             if (!used) {
4749edc8742Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
475ee091cf1Smatthias.ringwald             }
476afde0c52Smatthias.ringwald             break;
477ee091cf1Smatthias.ringwald 
478afde0c52Smatthias.ringwald         default:
479afde0c52Smatthias.ringwald             break;
480afde0c52Smatthias.ringwald     }
481afde0c52Smatthias.ringwald 
482afde0c52Smatthias.ringwald     // pass on
483b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
4841e6aba47Smatthias.ringwald }
4851e6aba47Smatthias.ringwald 
486afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
487*b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
488e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
489e7ff783cSmatthias.ringwald     l2cap_run();
49084836b65Smatthias.ringwald }
49184836b65Smatthias.ringwald 
492b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
493645658c9Smatthias.ringwald 
494fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
495645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
496645658c9Smatthias.ringwald     if (!service) {
497645658c9Smatthias.ringwald         // 0x0002 PSM not supported
498fe35119dSmatthias.ringwald         // log_dbg("l2cap_handle_connection_request no PSM for psm %u/n", psm);
499645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
500645658c9Smatthias.ringwald         return;
501645658c9Smatthias.ringwald     }
502645658c9Smatthias.ringwald 
503645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
504645658c9Smatthias.ringwald     if (!hci_connection) {
505fe35119dSmatthias.ringwald         log_err("no hci_connection for handle %u\n", handle);
506645658c9Smatthias.ringwald         // TODO: emit error
507645658c9Smatthias.ringwald         return;
508645658c9Smatthias.ringwald     }
509645658c9Smatthias.ringwald     // alloc structure
510fe35119dSmatthias.ringwald     // log_dbg("l2cap_handle_connection_request register channel\n");
511169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
512645658c9Smatthias.ringwald     // TODO: emit error event
513169f8b28Smatthias.ringwald     if (!channel) return;
514645658c9Smatthias.ringwald 
515645658c9Smatthias.ringwald     // fill in
516169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
517169f8b28Smatthias.ringwald     channel->psm = psm;
518169f8b28Smatthias.ringwald     channel->handle = handle;
519169f8b28Smatthias.ringwald     channel->connection = service->connection;
520f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
521b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
522b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
523fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
5240a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
525761b0451Smatthias.ringwald     channel->packets_granted = 0;
526*b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
527645658c9Smatthias.ringwald 
5281d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
5291d279b20Smatthias.ringwald     if (channel->local_mtu > hci_max_acl_data_packet_length()) {
5309775e25bSmatthias.ringwald         channel->local_mtu = hci_max_acl_data_packet_length();
5319775e25bSmatthias.ringwald     }
5329775e25bSmatthias.ringwald 
533645658c9Smatthias.ringwald     // set initial state
534e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
535e405ae81Smatthias.ringwald 
536645658c9Smatthias.ringwald     // add to connections list
537169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
538645658c9Smatthias.ringwald 
539e405ae81Smatthias.ringwald     // emit incoming connection request
540e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
541e405ae81Smatthias.ringwald }
542645658c9Smatthias.ringwald 
543b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
544b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
545e405ae81Smatthias.ringwald     if (!channel) {
546fe35119dSmatthias.ringwald         log_err("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
547e405ae81Smatthias.ringwald         return;
548e405ae81Smatthias.ringwald     }
549e405ae81Smatthias.ringwald 
550552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
551e405ae81Smatthias.ringwald 
552552d92a1Smatthias.ringwald     // process
553552d92a1Smatthias.ringwald     l2cap_run();
554e405ae81Smatthias.ringwald }
555645658c9Smatthias.ringwald 
556b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
557b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
558e405ae81Smatthias.ringwald     if (!channel) {
559fe35119dSmatthias.ringwald         log_err( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
560e405ae81Smatthias.ringwald         return;
561e405ae81Smatthias.ringwald     }
562e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
563e7ff783cSmatthias.ringwald     channel->reason = reason;
564e7ff783cSmatthias.ringwald     l2cap_run();
565645658c9Smatthias.ringwald }
566645658c9Smatthias.ringwald 
5672784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
568*b1988dceSmatthias.ringwald 
569*b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
570*b1988dceSmatthias.ringwald 
5712784b77dSmatthias.ringwald     // accept the other's configuration options
5723de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
5733de7c0caSmatthias.ringwald     uint16_t pos     = 8;
5743de7c0caSmatthias.ringwald     while (pos < end_pos){
5751dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
5761dc511deSmatthias.ringwald         uint8_t length = command[pos++];
5771dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
5781dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
5791dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
580fe35119dSmatthias.ringwald             // log_dbg("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
5811dc511deSmatthias.ringwald         }
5821dc511deSmatthias.ringwald         pos += length;
5831dc511deSmatthias.ringwald     }
5842784b77dSmatthias.ringwald }
5852784b77dSmatthias.ringwald 
58600d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
5871e6aba47Smatthias.ringwald 
58800d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
58900d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
59038e5900eSmatthias.ringwald     uint16_t result = 0;
5911e6aba47Smatthias.ringwald 
592fe35119dSmatthias.ringwald     // log_dbg("signaling handler code %u\n", code);
593b35f641cSmatthias.ringwald 
5941e6aba47Smatthias.ringwald     switch (channel->state) {
5951e6aba47Smatthias.ringwald 
5961e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
5971e6aba47Smatthias.ringwald             switch (code){
5981e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
59900d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
60038e5900eSmatthias.ringwald                     switch (result) {
60138e5900eSmatthias.ringwald                         case 0:
602169f8b28Smatthias.ringwald                             // successful connection
60300d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
604ae280e73Smatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ;
605d418e203Smatthias.ringwald #if 0
606d418e203Smatthias.ringwald     channel->state = L2CAP_STATE_OPEN;
607d418e203Smatthias.ringwald     l2cap_emit_channel_opened(channel, 0);  // success
608d418e203Smatthias.ringwald     l2cap_emit_credits(channel, 1);
609d418e203Smatthias.ringwald #endif
61038e5900eSmatthias.ringwald                             break;
61138e5900eSmatthias.ringwald                         case 1:
61238e5900eSmatthias.ringwald                             // connection pending. get some coffee
61338e5900eSmatthias.ringwald                             break;
61438e5900eSmatthias.ringwald                         default:
615eb920dbeSmatthias.ringwald                             // channel closed
616eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
617eb920dbeSmatthias.ringwald 
618f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
61938e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
620eb920dbeSmatthias.ringwald 
621eb920dbeSmatthias.ringwald                             // drop link key if security block
622eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
623eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
624eb920dbeSmatthias.ringwald                             }
625eb920dbeSmatthias.ringwald 
626eb920dbeSmatthias.ringwald                             // discard channel
627eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
628eb920dbeSmatthias.ringwald                             free (channel);
62938e5900eSmatthias.ringwald                             break;
6301e6aba47Smatthias.ringwald                     }
6311e6aba47Smatthias.ringwald                     break;
63238e5900eSmatthias.ringwald 
63384836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
63484836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
63584836b65Smatthias.ringwald                     break;
63684836b65Smatthias.ringwald 
63738e5900eSmatthias.ringwald                 default:
6381e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
63938e5900eSmatthias.ringwald                     break;
6401e6aba47Smatthias.ringwald             }
6411e6aba47Smatthias.ringwald             break;
6421e6aba47Smatthias.ringwald 
643ae280e73Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_OR_SEND_CONFIG_REQ:
644ae280e73Smatthias.ringwald             switch (code) {
645ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
646ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
647ae280e73Smatthias.ringwald                     channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ_AND_CONFIG_REQ_RSP;
648ae280e73Smatthias.ringwald                     break;
649ae280e73Smatthias.ringwald                 case DISCONNECTION_REQUEST:
650ae280e73Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
651ae280e73Smatthias.ringwald                     break;
652ae280e73Smatthias.ringwald                 default:
653ae280e73Smatthias.ringwald                     //@TODO: implement other signaling packets
654ae280e73Smatthias.ringwald                     break;
655ae280e73Smatthias.ringwald             }
656ae280e73Smatthias.ringwald             break;
657ae280e73Smatthias.ringwald 
6585a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
6591e6aba47Smatthias.ringwald             switch (code) {
6601e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
6611e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
6621e6aba47Smatthias.ringwald                     break;
6635a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
6642784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
665552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_WILL_SEND_CONFIG_REQ_RSP;
6665a67bd4aSmatthias.ringwald                     break;
66784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
66884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
66984836b65Smatthias.ringwald                     break;
6705a67bd4aSmatthias.ringwald                 default:
6715a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6725a67bd4aSmatthias.ringwald                     break;
6731e6aba47Smatthias.ringwald             }
6741e6aba47Smatthias.ringwald             break;
6751e6aba47Smatthias.ringwald 
6761e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
6771e6aba47Smatthias.ringwald             switch (code) {
6781e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
6792784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
680552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_WILL_SEND_CONFIG_REQ_RSP;
681c8e4258aSmatthias.ringwald                     break;
68284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
68384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
68484836b65Smatthias.ringwald                     break;
6855a67bd4aSmatthias.ringwald                 default:
6865a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6875a67bd4aSmatthias.ringwald                     break;
6885a67bd4aSmatthias.ringwald             }
6895a67bd4aSmatthias.ringwald             break;
6905a67bd4aSmatthias.ringwald 
6915a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
6925a67bd4aSmatthias.ringwald             switch (code) {
6935a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
6945a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
6955a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
6966218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
6975a67bd4aSmatthias.ringwald                     break;
69884836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
69984836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
70084836b65Smatthias.ringwald                     break;
7015a67bd4aSmatthias.ringwald                 default:
7025a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7035a67bd4aSmatthias.ringwald                     break;
704c8e4258aSmatthias.ringwald             }
705c8e4258aSmatthias.ringwald             break;
706f62db1e3Smatthias.ringwald 
707f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
708f62db1e3Smatthias.ringwald             switch (code) {
709f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
71027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
71127a923d0Smatthias.ringwald                     break;
71284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
71384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
71484836b65Smatthias.ringwald                     break;
7155a67bd4aSmatthias.ringwald                 default:
7165a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
7175a67bd4aSmatthias.ringwald                     break;
71827a923d0Smatthias.ringwald             }
71927a923d0Smatthias.ringwald             break;
72084836b65Smatthias.ringwald 
72184836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
72284836b65Smatthias.ringwald             // @TODO handle incoming requests
72384836b65Smatthias.ringwald             break;
72484836b65Smatthias.ringwald 
72584836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
72684836b65Smatthias.ringwald             switch (code) {
72784836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
72884836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
72984836b65Smatthias.ringwald                     break;
7305a67bd4aSmatthias.ringwald                 default:
73184836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
73284836b65Smatthias.ringwald                     break;
73384836b65Smatthias.ringwald             }
7345a67bd4aSmatthias.ringwald             break;
73510642e45Smatthias.ringwald         default:
73610642e45Smatthias.ringwald             break;
73727a923d0Smatthias.ringwald     }
738fe35119dSmatthias.ringwald     // log_dbg("new state %u\n", channel->state);
73927a923d0Smatthias.ringwald }
74027a923d0Smatthias.ringwald 
74100d93d79Smatthias.ringwald 
74200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
74300d93d79Smatthias.ringwald 
74400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
74500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
74600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
74700d93d79Smatthias.ringwald     uint16_t len   = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
74800d93d79Smatthias.ringwald 
74900d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
75000d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
75100d93d79Smatthias.ringwald         return;
75200d93d79Smatthias.ringwald     }
75300d93d79Smatthias.ringwald 
75400d93d79Smatthias.ringwald     // general commands without an assigned channel
75500d93d79Smatthias.ringwald     switch(code) {
75600d93d79Smatthias.ringwald 
75700d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
75800d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
75900d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
76000d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
76100d93d79Smatthias.ringwald             break;
76200d93d79Smatthias.ringwald         }
76300d93d79Smatthias.ringwald 
76400d93d79Smatthias.ringwald         case ECHO_REQUEST: {
76500d93d79Smatthias.ringwald             // send back packet
76600d93d79Smatthias.ringwald             l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]);
76700d93d79Smatthias.ringwald             break;
76800d93d79Smatthias.ringwald         }
76900d93d79Smatthias.ringwald 
77000d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
77100d93d79Smatthias.ringwald             // we neither support connectionless L2CAP data nor support any flow control modes yet
77200d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
77300d93d79Smatthias.ringwald             if (infoType == 2) {
77400d93d79Smatthias.ringwald                 uint32_t features = 0;
77500d93d79Smatthias.ringwald                 // extended features request supported, however no features present
77600d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
77700d93d79Smatthias.ringwald             } else {
77800d93d79Smatthias.ringwald                 // all other types are not supported
77900d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
78000d93d79Smatthias.ringwald             }
78100d93d79Smatthias.ringwald             break;;
78200d93d79Smatthias.ringwald         }
78300d93d79Smatthias.ringwald 
78400d93d79Smatthias.ringwald         default:
78500d93d79Smatthias.ringwald             break;
78600d93d79Smatthias.ringwald     }
78700d93d79Smatthias.ringwald 
78800d93d79Smatthias.ringwald 
78900d93d79Smatthias.ringwald     // Get potential destination CID
79000d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
79100d93d79Smatthias.ringwald 
79200d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
79300d93d79Smatthias.ringwald     linked_item_t *it;
79400d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
79500d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
79600d93d79Smatthias.ringwald         if (channel->handle == handle) {
79700d93d79Smatthias.ringwald             if (code & 1) {
798*b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
799*b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
80000d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8014e32727eSmatthias.ringwald                     break;
80200d93d79Smatthias.ringwald                 }
80300d93d79Smatthias.ringwald             } else {
804*b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
80500d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
80600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8074e32727eSmatthias.ringwald                     break;
80800d93d79Smatthias.ringwald                 }
80900d93d79Smatthias.ringwald             }
81000d93d79Smatthias.ringwald         }
81100d93d79Smatthias.ringwald     }
81200d93d79Smatthias.ringwald }
81300d93d79Smatthias.ringwald 
81400d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
81500d93d79Smatthias.ringwald 
81600d93d79Smatthias.ringwald     // Get Channel ID
81700d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
81800d93d79Smatthias.ringwald 
81900d93d79Smatthias.ringwald     // Signaling Packet?
82000d93d79Smatthias.ringwald     if (channel_id == 1) {
82100d93d79Smatthias.ringwald 
82200d93d79Smatthias.ringwald         // Get Connection
82300d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
82400d93d79Smatthias.ringwald 
82500d93d79Smatthias.ringwald         uint16_t command_offset = 8;
82600d93d79Smatthias.ringwald         while (command_offset < size) {
82700d93d79Smatthias.ringwald 
82800d93d79Smatthias.ringwald             // handle signaling commands
82900d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
83000d93d79Smatthias.ringwald 
83100d93d79Smatthias.ringwald             // increment command_offset
83200d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
83300d93d79Smatthias.ringwald         }
83400d93d79Smatthias.ringwald         return;
83500d93d79Smatthias.ringwald     }
83600d93d79Smatthias.ringwald 
83700d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
83800d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
83900d93d79Smatthias.ringwald     if (channel) {
84058de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
84100d93d79Smatthias.ringwald     }
84200d93d79Smatthias.ringwald }
84300d93d79Smatthias.ringwald 
8442718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
8452718e2e7Smatthias.ringwald     switch (packet_type) {
8462718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
8472718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
8482718e2e7Smatthias.ringwald             break;
8492718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
8502718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
8512718e2e7Smatthias.ringwald             break;
8522718e2e7Smatthias.ringwald         default:
8532718e2e7Smatthias.ringwald             break;
8542718e2e7Smatthias.ringwald     }
8552718e2e7Smatthias.ringwald }
85600d93d79Smatthias.ringwald 
85715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
85827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
859f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
860f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
861f62db1e3Smatthias.ringwald     // discard channel
862f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
863f62db1e3Smatthias.ringwald     free (channel);
864c8e4258aSmatthias.ringwald }
8651e6aba47Smatthias.ringwald 
8669d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
867c52bf64dSmatthias.ringwald     linked_item_t *it;
8689d9bbc01Smatthias.ringwald 
8699d9bbc01Smatthias.ringwald     // close open channels
8709d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
8719d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
8729d9bbc01Smatthias.ringwald         if ( service->psm == psm){
8739d9bbc01Smatthias.ringwald             return service;
8749d9bbc01Smatthias.ringwald         };
8759d9bbc01Smatthias.ringwald     }
8769d9bbc01Smatthias.ringwald     return NULL;
8779d9bbc01Smatthias.ringwald }
8789d9bbc01Smatthias.ringwald 
87936944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
8809d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
8819d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
8829d9bbc01Smatthias.ringwald     if (service) return;
8839d9bbc01Smatthias.ringwald 
8849d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
8859d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
8869d9bbc01Smatthias.ringwald     if (!service) return;
8879d9bbc01Smatthias.ringwald 
8889d9bbc01Smatthias.ringwald     // fill in
8899d9bbc01Smatthias.ringwald     service->psm = psm;
8909d9bbc01Smatthias.ringwald     service->mtu = mtu;
8919d9bbc01Smatthias.ringwald     service->connection = connection;
892d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
8939d9bbc01Smatthias.ringwald 
8949d9bbc01Smatthias.ringwald     // add to services list
8959d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
8969d9bbc01Smatthias.ringwald }
8979d9bbc01Smatthias.ringwald 
89836944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
8999d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
900037d6e48Smatthias.ringwald     if (!service) return;
9019d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
9029d9bbc01Smatthias.ringwald     free(service);
9039d9bbc01Smatthias.ringwald }
9049d9bbc01Smatthias.ringwald 
9059d9bbc01Smatthias.ringwald //
90636944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
9079d9bbc01Smatthias.ringwald     linked_item_t *it;
9089d9bbc01Smatthias.ringwald 
9093a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
910c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
911c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
912c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
913c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
914cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
915c52bf64dSmatthias.ringwald         }
916c52bf64dSmatthias.ringwald     }
9179d9bbc01Smatthias.ringwald 
918645658c9Smatthias.ringwald     // unregister services
91969025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
92069025de8Smatthias.ringwald     while (it->next) {
92169025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
922645658c9Smatthias.ringwald         if (service->connection == connection){
92369025de8Smatthias.ringwald             it->next = it->next->next;
9248149d2c7Smatthias.ringwald             free(service);
9258149d2c7Smatthias.ringwald         } else {
9268149d2c7Smatthias.ringwald             it = it->next;
927645658c9Smatthias.ringwald         }
928645658c9Smatthias.ringwald     }
929cb8eb7e6Smatthias.ringwald 
930cb8eb7e6Smatthias.ringwald     // process
931cb8eb7e6Smatthias.ringwald     l2cap_run();
932c52bf64dSmatthias.ringwald }
933c52bf64dSmatthias.ringwald