xref: /btstack/src/l2cap.c (revision 6218e6f12b65360502381e49940a9ac8941192a4)
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"
43*6218e6f1Smatthias.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 
5700d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
5800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
5900d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6000d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6100d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6200d93d79Smatthias.ringwald 
6336944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
642718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
65fcadd0caSmatthias.ringwald 
661e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL;
671e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
689d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
691e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL;
7036944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
711e6aba47Smatthias.ringwald 
72169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48
73169f8b28Smatthias.ringwald 
741e6aba47Smatthias.ringwald void l2cap_init(){
752784b77dSmatthias.ringwald     sig_buffer = malloc( L2CAP_MINIMAL_MTU );
7611c41d51Smatthias.ringwald     acl_buffer = malloc( HCI_ACL_3DH5_SIZE);
77fcadd0caSmatthias.ringwald 
78fcadd0caSmatthias.ringwald     //
792718e2e7Smatthias.ringwald     // register callback with HCI
80fcadd0caSmatthias.ringwald     //
812718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
82fcadd0caSmatthias.ringwald }
83fcadd0caSmatthias.ringwald 
84fcadd0caSmatthias.ringwald 
85fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
8636944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
87fcadd0caSmatthias.ringwald }
8836944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
89b502e1b0Smatthias.ringwald     packet_handler = handler;
901e6aba47Smatthias.ringwald }
911e6aba47Smatthias.ringwald 
9258de5610Smatthias.ringwald //  notify client/protocol handler
9358de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
9458de5610Smatthias.ringwald     if (channel->packet_handler) {
9558de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
9658de5610Smatthias.ringwald     } else {
9736944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
9858de5610Smatthias.ringwald     }
9958de5610Smatthias.ringwald }
10058de5610Smatthias.ringwald 
10158de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
10258de5610Smatthias.ringwald     uint8_t event[17];
10358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
10458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
10558de5610Smatthias.ringwald     event[2] = status;
10658de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
10758de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
10858de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
10958de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
11058de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
11158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
11258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
11358de5610Smatthias.ringwald }
11458de5610Smatthias.ringwald 
11558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
11658de5610Smatthias.ringwald     uint8_t event[4];
11758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
11858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
11958de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
12058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
12158de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
12258de5610Smatthias.ringwald }
12358de5610Smatthias.ringwald 
12458de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
12558de5610Smatthias.ringwald     uint8_t event[16];
12658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
12758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12858de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
12958de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
13058de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
13158de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
13258de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
13358de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13458de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1350af41d30Smatthias.ringwald }
1360af41d30Smatthias.ringwald 
137*6218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
138*6218e6f1Smatthias.ringwald     // first check if we should hand out credits
139*6218e6f1Smatthias.ringwald 
140*6218e6f1Smatthias.ringwald     if (!hci_number_free_acl_slots() || hci_number_outgoing_packets(channel->handle) >= 2){
141*6218e6f1Smatthias.ringwald         return; // not yet
142*6218e6f1Smatthias.ringwald     }
143*6218e6f1Smatthias.ringwald     // track credits
144*6218e6f1Smatthias.ringwald     channel->packets_granted += credits;
145*6218e6f1Smatthias.ringwald     log_dbg("l2cap_emit_credits for cid %u, credits now: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
146*6218e6f1Smatthias.ringwald 
147*6218e6f1Smatthias.ringwald     uint8_t event[5];
148*6218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
149*6218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
150*6218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
151*6218e6f1Smatthias.ringwald     event[4] = credits;
152*6218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
153*6218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
154*6218e6f1Smatthias.ringwald }
155*6218e6f1Smatthias.ringwald 
156b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
157f62db1e3Smatthias.ringwald     linked_item_t *it;
158f62db1e3Smatthias.ringwald     l2cap_channel_t * channel;
159f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
160f62db1e3Smatthias.ringwald         channel = (l2cap_channel_t *) it;
161b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
162f62db1e3Smatthias.ringwald             return channel;
163f62db1e3Smatthias.ringwald         }
164f62db1e3Smatthias.ringwald     }
165f62db1e3Smatthias.ringwald     return NULL;
166f62db1e3Smatthias.ringwald }
167f62db1e3Smatthias.ringwald 
16896cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
16996cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
17096cbd662Smatthias.ringwald     if (channel) {
17196cbd662Smatthias.ringwald         return channel->remote_mtu;
17296cbd662Smatthias.ringwald     }
17396cbd662Smatthias.ringwald     return 0;
17496cbd662Smatthias.ringwald }
17596cbd662Smatthias.ringwald 
17658de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
17758de5610Smatthias.ringwald     // printf("l2cap_send_signaling_packet type %u\n", cmd);
17858de5610Smatthias.ringwald     va_list argptr;
17958de5610Smatthias.ringwald     va_start(argptr, identifier);
18058de5610Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr);
18158de5610Smatthias.ringwald     va_end(argptr);
18258de5610Smatthias.ringwald     return hci_send_acl_packet(sig_buffer, len);
18358de5610Smatthias.ringwald }
18458de5610Smatthias.ringwald 
185*6218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
186*6218e6f1Smatthias.ringwald 
187*6218e6f1Smatthias.ringwald     // check for free places on BT module
188*6218e6f1Smatthias.ringwald     if (!hci_number_free_acl_slots()) return -1;
189*6218e6f1Smatthias.ringwald 
190*6218e6f1Smatthias.ringwald     int err = 0;
191*6218e6f1Smatthias.ringwald 
19258de5610Smatthias.ringwald     // find channel for local_cid, construct l2cap packet and send
19358de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
19458de5610Smatthias.ringwald     if (channel) {
195*6218e6f1Smatthias.ringwald         // track sending
196*6218e6f1Smatthias.ringwald         ++channel->packets_outgoing;
197*6218e6f1Smatthias.ringwald         if (channel->packets_granted > 0){
198*6218e6f1Smatthias.ringwald             --channel->packets_granted;
199*6218e6f1Smatthias.ringwald         } else {
200*6218e6f1Smatthias.ringwald             log_err("l2cap_send_internal cid %u, no credits!\n", local_cid);
201*6218e6f1Smatthias.ringwald         }
202*6218e6f1Smatthias.ringwald         log_dbg("l2cap_send_internal cid %u, 1 credit used, credits left %u; outgoing count %u\n",
203*6218e6f1Smatthias.ringwald                 local_cid, channel->packets_granted, channel->packets_outgoing);
204*6218e6f1Smatthias.ringwald 
20558de5610Smatthias.ringwald         // 0 - Connection handle : PB=10 : BC=00
20658de5610Smatthias.ringwald         bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
20758de5610Smatthias.ringwald         // 2 - ACL length
20858de5610Smatthias.ringwald         bt_store_16(acl_buffer, 2,  len + 4);
20958de5610Smatthias.ringwald         // 4 - L2CAP packet length
21058de5610Smatthias.ringwald         bt_store_16(acl_buffer, 4,  len + 0);
21158de5610Smatthias.ringwald         // 6 - L2CAP channel DEST
21258de5610Smatthias.ringwald         bt_store_16(acl_buffer, 6, channel->remote_cid);
21358de5610Smatthias.ringwald         // 8 - data
21458de5610Smatthias.ringwald         memcpy(&acl_buffer[8], data, len);
215*6218e6f1Smatthias.ringwald 
21658de5610Smatthias.ringwald         // send
217*6218e6f1Smatthias.ringwald         err = hci_send_acl_packet(acl_buffer, len+8);
21858de5610Smatthias.ringwald     }
219*6218e6f1Smatthias.ringwald     return err;
22058de5610Smatthias.ringwald }
22158de5610Smatthias.ringwald 
2221e6aba47Smatthias.ringwald // open outgoing L2CAP channel
2236b296a27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm){
2241e6aba47Smatthias.ringwald 
2251e6aba47Smatthias.ringwald     // alloc structure
2261e6aba47Smatthias.ringwald     l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t));
2271e6aba47Smatthias.ringwald     // TODO: emit error event
2281e6aba47Smatthias.ringwald     if (!chan) return;
2291e6aba47Smatthias.ringwald 
2301e6aba47Smatthias.ringwald     // fill in
2311e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
2321e6aba47Smatthias.ringwald     chan->psm = psm;
2331e6aba47Smatthias.ringwald     chan->handle = 0;
2341e6aba47Smatthias.ringwald     chan->connection = connection;
2356b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
2362784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
2371e6aba47Smatthias.ringwald 
238*6218e6f1Smatthias.ringwald     // flow control
239*6218e6f1Smatthias.ringwald     chan->packets_granted = 0;
240*6218e6f1Smatthias.ringwald     chan->packets_outgoing = 0;
241*6218e6f1Smatthias.ringwald 
2421e6aba47Smatthias.ringwald     // set initial state
2431e6aba47Smatthias.ringwald     chan->state = L2CAP_STATE_CLOSED;
2441e6aba47Smatthias.ringwald     chan->sig_id = L2CAP_SIG_ID_INVALID;
2451e6aba47Smatthias.ringwald 
2461e6aba47Smatthias.ringwald     // add to connections list
2471e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
2481e6aba47Smatthias.ringwald 
2491e6aba47Smatthias.ringwald     // send connection request
2501e6aba47Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
2511e6aba47Smatthias.ringwald     hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0);
25243625864Smatthias.ringwald }
25343625864Smatthias.ringwald 
254b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
255b35f641cSmatthias.ringwald     // find channel for local_cid
256b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
257f62db1e3Smatthias.ringwald     if (channel) {
258f62db1e3Smatthias.ringwald         channel->sig_id = l2cap_next_sig_id();
259b35f641cSmatthias.ringwald         l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
260f62db1e3Smatthias.ringwald         channel->state = L2CAP_STATE_WAIT_DISCONNECT;
261f62db1e3Smatthias.ringwald     }
26243625864Smatthias.ringwald }
2631e6aba47Smatthias.ringwald 
264afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
2651e6aba47Smatthias.ringwald     linked_item_t *it;
2661e6aba47Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
267b448a0e7Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
268b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
269b448a0e7Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
270afde0c52Smatthias.ringwald                 // failure, forward error code
271afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
272afde0c52Smatthias.ringwald                 // discard channel
273afde0c52Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
274afde0c52Smatthias.ringwald                 free (channel);
275afde0c52Smatthias.ringwald             }
276afde0c52Smatthias.ringwald         }
277afde0c52Smatthias.ringwald     }
278afde0c52Smatthias.ringwald }
279afde0c52Smatthias.ringwald 
280afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
281afde0c52Smatthias.ringwald     linked_item_t *it;
282afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
283afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
284afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
285afde0c52Smatthias.ringwald             if (channel->state == L2CAP_STATE_CLOSED) {
286b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
287afde0c52Smatthias.ringwald                 channel->handle = handle;
288b448a0e7Smatthias.ringwald                 channel->sig_id = l2cap_next_sig_id();
289b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
290b448a0e7Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
291b35f641cSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid);
292afde0c52Smatthias.ringwald             }
293afde0c52Smatthias.ringwald         }
294afde0c52Smatthias.ringwald     }
295afde0c52Smatthias.ringwald }
296b448a0e7Smatthias.ringwald 
297afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
298afde0c52Smatthias.ringwald 
299afde0c52Smatthias.ringwald     bd_addr_t address;
300afde0c52Smatthias.ringwald     hci_con_handle_t handle;
30136944dffSmatthias.ringwald     linked_item_t *it;
302*6218e6f1Smatthias.ringwald     int i;
303afde0c52Smatthias.ringwald 
304afde0c52Smatthias.ringwald     switch(packet[0]){
305afde0c52Smatthias.ringwald 
306afde0c52Smatthias.ringwald         // handle connection complete events
307afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
308afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
309afde0c52Smatthias.ringwald             if (packet[2] == 0){
310afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
311afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
312afde0c52Smatthias.ringwald             } else {
313afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
314afde0c52Smatthias.ringwald             }
315afde0c52Smatthias.ringwald             break;
316afde0c52Smatthias.ringwald 
317afde0c52Smatthias.ringwald         // handle successful create connection cancel command
318afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
319afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
320afde0c52Smatthias.ringwald                 if (packet[5] == 0){
321afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
322afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
323afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
32403cfbabcSmatthias.ringwald                 }
3251e6aba47Smatthias.ringwald             }
326afde0c52Smatthias.ringwald             break;
32727a923d0Smatthias.ringwald 
3281e6aba47Smatthias.ringwald         // handle disconnection complete events
329afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
33027a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
331afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
33239ce35a7Smatthias.ringwald             // only access next element to allows for removal
33378a6f100Smatthias.ringwald             for (it = (linked_item_t *) &l2cap_channels; it->next ; it = it->next){
33439ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
33527a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
33639ce35a7Smatthias.ringwald                     // update prev item before free'ing next element
33739ce35a7Smatthias.ringwald                     it->next = it->next->next;
33827a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
33927a923d0Smatthias.ringwald                 }
34027a923d0Smatthias.ringwald             }
341afde0c52Smatthias.ringwald             break;
342fcadd0caSmatthias.ringwald 
343*6218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
344*6218e6f1Smatthias.ringwald             for (i=0; i<packet[2];i++){
345*6218e6f1Smatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
346*6218e6f1Smatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
347*6218e6f1Smatthias.ringwald                 while (num_packets) {
348*6218e6f1Smatthias.ringwald                     num_packets--;
349*6218e6f1Smatthias.ringwald                     // pick some slot
350*6218e6f1Smatthias.ringwald                     l2cap_channel_t * oldest_channel = NULL;
351*6218e6f1Smatthias.ringwald                     for (it = (linked_item_t *) &l2cap_channels; it->next ; it = it->next){
352*6218e6f1Smatthias.ringwald                         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
353*6218e6f1Smatthias.ringwald                         if (channel->packets_outgoing){
354*6218e6f1Smatthias.ringwald                             oldest_channel   = channel;
355*6218e6f1Smatthias.ringwald                         }
356*6218e6f1Smatthias.ringwald                     }
357*6218e6f1Smatthias.ringwald                     // decrease packets and grant new credits
358*6218e6f1Smatthias.ringwald                     if (oldest_channel) {
359*6218e6f1Smatthias.ringwald                         oldest_channel->packets_outgoing--;
360*6218e6f1Smatthias.ringwald 
361*6218e6f1Smatthias.ringwald                         log_dbg("hci_number_completed_packet (l2cap) for cid %u, outgoing count %u\n",
362*6218e6f1Smatthias.ringwald                                 oldest_channel->local_cid, oldest_channel->packets_outgoing);
363*6218e6f1Smatthias.ringwald 
364*6218e6f1Smatthias.ringwald                     } else {
365*6218e6f1Smatthias.ringwald                         log_err("hci_number_completed_packet but no outgoing packet in records\n");
366*6218e6f1Smatthias.ringwald                     }
367*6218e6f1Smatthias.ringwald                 }
368*6218e6f1Smatthias.ringwald             }
369*6218e6f1Smatthias.ringwald             break;
370*6218e6f1Smatthias.ringwald 
371ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
372afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
37336944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
374ee091cf1Smatthias.ringwald             l2cap_channel_t * channel;
375ee091cf1Smatthias.ringwald             int used = 0;
376ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
377ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
378ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
379ee091cf1Smatthias.ringwald                     used = 1;
380ee091cf1Smatthias.ringwald                 }
381ee091cf1Smatthias.ringwald             }
382ee091cf1Smatthias.ringwald             if (!used) {
3839edc8742Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
384ee091cf1Smatthias.ringwald             }
385afde0c52Smatthias.ringwald             break;
386ee091cf1Smatthias.ringwald 
387afde0c52Smatthias.ringwald         default:
388afde0c52Smatthias.ringwald             break;
389afde0c52Smatthias.ringwald     }
390afde0c52Smatthias.ringwald 
391afde0c52Smatthias.ringwald     // pass on
392b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
3931e6aba47Smatthias.ringwald }
3941e6aba47Smatthias.ringwald 
395afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
396f79ee244Smatthias.ringwald     l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->local_cid, channel->remote_cid);
39784836b65Smatthias.ringwald     l2cap_finialize_channel_close(channel);
39884836b65Smatthias.ringwald }
39984836b65Smatthias.ringwald 
400b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
401645658c9Smatthias.ringwald 
402b35f641cSmatthias.ringwald     // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
403645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
404645658c9Smatthias.ringwald     if (!service) {
405645658c9Smatthias.ringwald         // 0x0002 PSM not supported
406169f8b28Smatthias.ringwald         // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm);
407645658c9Smatthias.ringwald         l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0);
408645658c9Smatthias.ringwald         return;
409645658c9Smatthias.ringwald     }
410645658c9Smatthias.ringwald 
411645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
412645658c9Smatthias.ringwald     if (!hci_connection) {
413169f8b28Smatthias.ringwald         fprintf(stderr, "no hci_connection for handle %u\n", handle);
414645658c9Smatthias.ringwald         // TODO: emit error
415645658c9Smatthias.ringwald         return;
416645658c9Smatthias.ringwald     }
417645658c9Smatthias.ringwald     // alloc structure
418169f8b28Smatthias.ringwald     // printf("l2cap_handle_connection_request register channel\n");
419169f8b28Smatthias.ringwald     l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t));
420645658c9Smatthias.ringwald     // TODO: emit error event
421169f8b28Smatthias.ringwald     if (!channel) return;
422645658c9Smatthias.ringwald 
423645658c9Smatthias.ringwald     // fill in
424169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
425169f8b28Smatthias.ringwald     channel->psm = psm;
426169f8b28Smatthias.ringwald     channel->handle = handle;
427169f8b28Smatthias.ringwald     channel->connection = service->connection;
428f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
429b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
430b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
4312784b77dSmatthias.ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
432645658c9Smatthias.ringwald 
433645658c9Smatthias.ringwald     // set initial state
434e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
435e405ae81Smatthias.ringwald 
436e405ae81Smatthias.ringwald     // temp. store req sig id
437e405ae81Smatthias.ringwald     channel->sig_id = sig_id;
438645658c9Smatthias.ringwald 
439645658c9Smatthias.ringwald     // add to connections list
440169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
441645658c9Smatthias.ringwald 
442e405ae81Smatthias.ringwald     // emit incoming connection request
443e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
444e405ae81Smatthias.ringwald }
445645658c9Smatthias.ringwald 
446b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
447b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
448e405ae81Smatthias.ringwald     if (!channel) {
449b35f641cSmatthias.ringwald         fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
450e405ae81Smatthias.ringwald         return;
451e405ae81Smatthias.ringwald     }
452e405ae81Smatthias.ringwald 
453e405ae81Smatthias.ringwald     // accept connection
454b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0);
455e405ae81Smatthias.ringwald 
456e405ae81Smatthias.ringwald     // set real sig and state and start config
457e405ae81Smatthias.ringwald     channel->sig_id = l2cap_next_sig_id();
458e405ae81Smatthias.ringwald     channel->state  = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
459b35f641cSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
460b35f641cSmatthias.ringwald 
461b35f641cSmatthias.ringwald     // printf("new state %u\n", channel->state);
462e405ae81Smatthias.ringwald }
463645658c9Smatthias.ringwald 
464b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
465b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
466e405ae81Smatthias.ringwald     if (!channel) {
467b7de95daSmatthias.ringwald         fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
468e405ae81Smatthias.ringwald         return;
469e405ae81Smatthias.ringwald     }
470e405ae81Smatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0);
471e405ae81Smatthias.ringwald 
472e405ae81Smatthias.ringwald     // discard channel
473e405ae81Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
474e405ae81Smatthias.ringwald     free (channel);
475645658c9Smatthias.ringwald }
476645658c9Smatthias.ringwald 
4772784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
4782784b77dSmatthias.ringwald     // accept the other's configuration options
4793de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
4803de7c0caSmatthias.ringwald     uint16_t pos     = 8;
4813de7c0caSmatthias.ringwald     while (pos < end_pos){
4821dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
4831dc511deSmatthias.ringwald         uint8_t length = command[pos++];
4841dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
4851dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
4861dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
4873de7c0caSmatthias.ringwald             // printf("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
4881dc511deSmatthias.ringwald         }
4891dc511deSmatthias.ringwald         pos += length;
4901dc511deSmatthias.ringwald     }
4912784b77dSmatthias.ringwald     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
4921dc511deSmatthias.ringwald     // send back received options
4931dc511deSmatthias.ringwald     // l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]);
4941dc511deSmatthias.ringwald     // send back OK
4951dc511deSmatthias.ringwald     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, 0, NULL);
4962784b77dSmatthias.ringwald }
4972784b77dSmatthias.ringwald 
49800d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
4991e6aba47Smatthias.ringwald 
50000d93d79Smatthias.ringwald     uint8_t code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
50100d93d79Smatthias.ringwald     uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
50238e5900eSmatthias.ringwald     uint16_t result = 0;
5031e6aba47Smatthias.ringwald 
504b35f641cSmatthias.ringwald     // printf("signaling handler code %u\n", code);
505b35f641cSmatthias.ringwald 
5061e6aba47Smatthias.ringwald     switch (channel->state) {
5071e6aba47Smatthias.ringwald 
5081e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
5091e6aba47Smatthias.ringwald             switch (code){
5101e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
51100d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
51238e5900eSmatthias.ringwald                     switch (result) {
51338e5900eSmatthias.ringwald                         case 0:
514169f8b28Smatthias.ringwald                             // successful connection
51500d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
5161e6aba47Smatthias.ringwald                             channel->sig_id = l2cap_next_sig_id();
517b35f641cSmatthias.ringwald                             l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options);
5185a67bd4aSmatthias.ringwald                             channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ;
51938e5900eSmatthias.ringwald                             break;
52038e5900eSmatthias.ringwald                         case 1:
52138e5900eSmatthias.ringwald                             // connection pending. get some coffee
52238e5900eSmatthias.ringwald                             break;
52338e5900eSmatthias.ringwald                         default:
524f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
52538e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
52638e5900eSmatthias.ringwald                             break;
5271e6aba47Smatthias.ringwald                     }
5281e6aba47Smatthias.ringwald                     break;
52938e5900eSmatthias.ringwald 
53084836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
53184836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
53284836b65Smatthias.ringwald                     break;
53384836b65Smatthias.ringwald 
53438e5900eSmatthias.ringwald                 default:
5351e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
53638e5900eSmatthias.ringwald                     break;
5371e6aba47Smatthias.ringwald             }
5381e6aba47Smatthias.ringwald             break;
5391e6aba47Smatthias.ringwald 
5405a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ:
5411e6aba47Smatthias.ringwald             switch (code) {
5421e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
5431e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ;
5441e6aba47Smatthias.ringwald                     break;
5455a67bd4aSmatthias.ringwald                 case CONFIGURE_REQUEST:
5462784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
5475a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP;
5485a67bd4aSmatthias.ringwald                     break;
54984836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
55084836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
55184836b65Smatthias.ringwald                     break;
5525a67bd4aSmatthias.ringwald                 default:
5535a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
5545a67bd4aSmatthias.ringwald                     break;
5551e6aba47Smatthias.ringwald             }
5561e6aba47Smatthias.ringwald             break;
5571e6aba47Smatthias.ringwald 
5581e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ:
5591e6aba47Smatthias.ringwald             switch (code) {
5601e6aba47Smatthias.ringwald                 case CONFIGURE_REQUEST:
5612784b77dSmatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
5621e6aba47Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
56303cfbabcSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
564*6218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
565c8e4258aSmatthias.ringwald                     break;
56684836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
56784836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
56884836b65Smatthias.ringwald                     break;
5695a67bd4aSmatthias.ringwald                 default:
5705a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
5715a67bd4aSmatthias.ringwald                     break;
5725a67bd4aSmatthias.ringwald             }
5735a67bd4aSmatthias.ringwald             break;
5745a67bd4aSmatthias.ringwald 
5755a67bd4aSmatthias.ringwald         case L2CAP_STATE_WAIT_CONFIG_REQ_RSP:
5765a67bd4aSmatthias.ringwald             switch (code) {
5775a67bd4aSmatthias.ringwald                 case CONFIGURE_RESPONSE:
5785a67bd4aSmatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
5795a67bd4aSmatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
580*6218e6f1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
5815a67bd4aSmatthias.ringwald                     break;
58284836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
58384836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
58484836b65Smatthias.ringwald                     break;
5855a67bd4aSmatthias.ringwald                 default:
5865a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
5875a67bd4aSmatthias.ringwald                     break;
588c8e4258aSmatthias.ringwald             }
589c8e4258aSmatthias.ringwald             break;
590f62db1e3Smatthias.ringwald 
591f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
592f62db1e3Smatthias.ringwald             switch (code) {
593f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
59427a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
59527a923d0Smatthias.ringwald                     break;
59684836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
59784836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
59884836b65Smatthias.ringwald                     break;
5995a67bd4aSmatthias.ringwald                 default:
6005a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
6015a67bd4aSmatthias.ringwald                     break;
60227a923d0Smatthias.ringwald             }
60327a923d0Smatthias.ringwald             break;
60484836b65Smatthias.ringwald 
60584836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
60684836b65Smatthias.ringwald             // @TODO handle incoming requests
60784836b65Smatthias.ringwald             break;
60884836b65Smatthias.ringwald 
60984836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
61084836b65Smatthias.ringwald             switch (code) {
61184836b65Smatthias.ringwald                 case DISCONNECTION_REQUEST:
61284836b65Smatthias.ringwald                     l2cap_handle_disconnect_request(channel, identifier);
61384836b65Smatthias.ringwald                     break;
6145a67bd4aSmatthias.ringwald                 default:
61584836b65Smatthias.ringwald                     //@TODO: implement other signaling packets, e.g. re-configure
61684836b65Smatthias.ringwald                     break;
61784836b65Smatthias.ringwald             }
6185a67bd4aSmatthias.ringwald             break;
61910642e45Smatthias.ringwald         default:
62010642e45Smatthias.ringwald             break;
62127a923d0Smatthias.ringwald     }
622b35f641cSmatthias.ringwald     // printf("new state %u\n", channel->state);
62327a923d0Smatthias.ringwald }
62427a923d0Smatthias.ringwald 
62500d93d79Smatthias.ringwald 
62600d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
62700d93d79Smatthias.ringwald 
62800d93d79Smatthias.ringwald     // get code, signalind identifier and command len
62900d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
63000d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
63100d93d79Smatthias.ringwald     uint16_t len   = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
63200d93d79Smatthias.ringwald 
63300d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
63400d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
63500d93d79Smatthias.ringwald         return;
63600d93d79Smatthias.ringwald     }
63700d93d79Smatthias.ringwald 
63800d93d79Smatthias.ringwald     // general commands without an assigned channel
63900d93d79Smatthias.ringwald     switch(code) {
64000d93d79Smatthias.ringwald 
64100d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
64200d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
64300d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
64400d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
64500d93d79Smatthias.ringwald             break;
64600d93d79Smatthias.ringwald         }
64700d93d79Smatthias.ringwald 
64800d93d79Smatthias.ringwald         case ECHO_REQUEST: {
64900d93d79Smatthias.ringwald             // send back packet
65000d93d79Smatthias.ringwald             l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]);
65100d93d79Smatthias.ringwald             break;
65200d93d79Smatthias.ringwald         }
65300d93d79Smatthias.ringwald 
65400d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
65500d93d79Smatthias.ringwald             // we neither support connectionless L2CAP data nor support any flow control modes yet
65600d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
65700d93d79Smatthias.ringwald             if (infoType == 2) {
65800d93d79Smatthias.ringwald                 uint32_t features = 0;
65900d93d79Smatthias.ringwald                 // extended features request supported, however no features present
66000d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
66100d93d79Smatthias.ringwald             } else {
66200d93d79Smatthias.ringwald                 // all other types are not supported
66300d93d79Smatthias.ringwald                 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
66400d93d79Smatthias.ringwald             }
66500d93d79Smatthias.ringwald             break;;
66600d93d79Smatthias.ringwald         }
66700d93d79Smatthias.ringwald 
66800d93d79Smatthias.ringwald         default:
66900d93d79Smatthias.ringwald             break;
67000d93d79Smatthias.ringwald     }
67100d93d79Smatthias.ringwald 
67200d93d79Smatthias.ringwald 
67300d93d79Smatthias.ringwald     // Get potential destination CID
67400d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
67500d93d79Smatthias.ringwald 
67600d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
67700d93d79Smatthias.ringwald     linked_item_t *it;
67800d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
67900d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
68000d93d79Smatthias.ringwald         if (channel->handle == handle) {
68100d93d79Smatthias.ringwald             if (code & 1) {
68200d93d79Smatthias.ringwald                 // match odd commands by previous signaling identifier
68300d93d79Smatthias.ringwald                 if (channel->sig_id == sig_id) {
68400d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
68500d93d79Smatthias.ringwald                 }
68600d93d79Smatthias.ringwald             } else {
68700d93d79Smatthias.ringwald                 // match even commands by local channel id
68800d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
68900d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
69000d93d79Smatthias.ringwald                 }
69100d93d79Smatthias.ringwald             }
69200d93d79Smatthias.ringwald         }
69300d93d79Smatthias.ringwald     }
69400d93d79Smatthias.ringwald }
69500d93d79Smatthias.ringwald 
69600d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
69700d93d79Smatthias.ringwald 
69800d93d79Smatthias.ringwald     // Get Channel ID
69900d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
70000d93d79Smatthias.ringwald 
70100d93d79Smatthias.ringwald     // Signaling Packet?
70200d93d79Smatthias.ringwald     if (channel_id == 1) {
70300d93d79Smatthias.ringwald 
70400d93d79Smatthias.ringwald         // Get Connection
70500d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
70600d93d79Smatthias.ringwald 
70700d93d79Smatthias.ringwald         uint16_t command_offset = 8;
70800d93d79Smatthias.ringwald         while (command_offset < size) {
70900d93d79Smatthias.ringwald 
71000d93d79Smatthias.ringwald             // handle signaling commands
71100d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
71200d93d79Smatthias.ringwald 
71300d93d79Smatthias.ringwald             // increment command_offset
71400d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
71500d93d79Smatthias.ringwald         }
71600d93d79Smatthias.ringwald         return;
71700d93d79Smatthias.ringwald     }
71800d93d79Smatthias.ringwald 
71900d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
72000d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
72100d93d79Smatthias.ringwald     if (channel) {
72258de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
72300d93d79Smatthias.ringwald     }
72400d93d79Smatthias.ringwald }
72500d93d79Smatthias.ringwald 
7262718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
7272718e2e7Smatthias.ringwald     switch (packet_type) {
7282718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
7292718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
7302718e2e7Smatthias.ringwald             break;
7312718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
7322718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
7332718e2e7Smatthias.ringwald             break;
7342718e2e7Smatthias.ringwald         default:
7352718e2e7Smatthias.ringwald             break;
7362718e2e7Smatthias.ringwald     }
7372718e2e7Smatthias.ringwald }
73800d93d79Smatthias.ringwald 
73927a923d0Smatthias.ringwald // finalize closed channel
74027a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
741f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
742f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
743f62db1e3Smatthias.ringwald 
744f62db1e3Smatthias.ringwald     // discard channel
745f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
746f62db1e3Smatthias.ringwald     free (channel);
747c8e4258aSmatthias.ringwald }
7481e6aba47Smatthias.ringwald 
7499d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
750c52bf64dSmatthias.ringwald     linked_item_t *it;
7519d9bbc01Smatthias.ringwald 
7529d9bbc01Smatthias.ringwald     // close open channels
7539d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
7549d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
7559d9bbc01Smatthias.ringwald         if ( service->psm == psm){
7569d9bbc01Smatthias.ringwald             return service;
7579d9bbc01Smatthias.ringwald         };
7589d9bbc01Smatthias.ringwald     }
7599d9bbc01Smatthias.ringwald     return NULL;
7609d9bbc01Smatthias.ringwald }
7619d9bbc01Smatthias.ringwald 
76236944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
7639d9bbc01Smatthias.ringwald     // check for alread registered psm // TODO: emit error event
7649d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
7659d9bbc01Smatthias.ringwald     if (service) return;
7669d9bbc01Smatthias.ringwald 
7679d9bbc01Smatthias.ringwald     // alloc structure     // TODO: emit error event
7689d9bbc01Smatthias.ringwald     service = malloc(sizeof(l2cap_service_t));
7699d9bbc01Smatthias.ringwald     if (!service) return;
7709d9bbc01Smatthias.ringwald 
7719d9bbc01Smatthias.ringwald     // fill in
7729d9bbc01Smatthias.ringwald     service->psm = psm;
7739d9bbc01Smatthias.ringwald     service->mtu = mtu;
7749d9bbc01Smatthias.ringwald     service->connection = connection;
775d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
7769d9bbc01Smatthias.ringwald 
7779d9bbc01Smatthias.ringwald     // add to services list
7789d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
7799d9bbc01Smatthias.ringwald }
7809d9bbc01Smatthias.ringwald 
78136944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
7829d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
783037d6e48Smatthias.ringwald     if (!service) return;
7849d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
7859d9bbc01Smatthias.ringwald     free(service);
7869d9bbc01Smatthias.ringwald }
7879d9bbc01Smatthias.ringwald 
7889d9bbc01Smatthias.ringwald //
78936944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
7909d9bbc01Smatthias.ringwald     linked_item_t *it;
7919d9bbc01Smatthias.ringwald 
7929d9bbc01Smatthias.ringwald     // close open channels
793c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
794c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
795c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
796c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
797c52bf64dSmatthias.ringwald             channel->sig_id = l2cap_next_sig_id();
798b35f641cSmatthias.ringwald             l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid);
799c52bf64dSmatthias.ringwald             channel->state = L2CAP_STATE_WAIT_DISCONNECT;
800c52bf64dSmatthias.ringwald         }
801c52bf64dSmatthias.ringwald     }
8029d9bbc01Smatthias.ringwald 
803645658c9Smatthias.ringwald     // unregister services
80469025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
80569025de8Smatthias.ringwald     while (it->next){
80669025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
807645658c9Smatthias.ringwald         if (service->connection == connection){
80869025de8Smatthias.ringwald             it->next = it->next->next;
8098149d2c7Smatthias.ringwald             free(service);
8108149d2c7Smatthias.ringwald         } else {
8118149d2c7Smatthias.ringwald             it = it->next;
812645658c9Smatthias.ringwald         }
813645658c9Smatthias.ringwald     }
814c52bf64dSmatthias.ringwald }
815c52bf64dSmatthias.ringwald