xref: /btstack/src/l2cap.c (revision 5842b6d9259e3df39417386abd8ef09414c303f2)
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"
44d3a9df87Smatthias.ringwald #include "btstack_memory.h"
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include <stdarg.h>
4743625864Smatthias.ringwald #include <string.h>
4843625864Smatthias.ringwald 
4943625864Smatthias.ringwald #include <stdio.h>
5043625864Smatthias.ringwald 
514c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
524c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
534c744e21Smatthias.ringwald 
5439bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
55e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
5639bda6d5Smatthias.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 
6339bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
6439bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
6539bda6d5Smatthias.ringwald 
6639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
672b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
682b83fb7dSmatthias.ringwald static int signaling_responses_pending;
692b83fb7dSmatthias.ringwald 
701e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
719d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
7236944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
73808a48abSmatthias.ringwald static int new_credits_blocked = 0;
741e6aba47Smatthias.ringwald 
7539bda6d5Smatthias.ringwald // prototypes
76fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
77fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
78fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
79fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
80fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
81fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8239bda6d5Smatthias.ringwald 
8339bda6d5Smatthias.ringwald 
841e6aba47Smatthias.ringwald void l2cap_init(){
85808a48abSmatthias.ringwald     new_credits_blocked = 0;
862b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
87808a48abSmatthias.ringwald 
88f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
89f5454fc6Smatthias.ringwald     l2cap_services = NULL;
90f5454fc6Smatthias.ringwald 
91f5454fc6Smatthias.ringwald     packet_handler = null_packet_handler;
92f5454fc6Smatthias.ringwald 
93fcadd0caSmatthias.ringwald     //
942718e2e7Smatthias.ringwald     // register callback with HCI
95fcadd0caSmatthias.ringwald     //
962718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
97c0e866bfSmatthias.ringwald     hci_connectable_control(0); // no services yet
98fcadd0caSmatthias.ringwald }
99fcadd0caSmatthias.ringwald 
100fcadd0caSmatthias.ringwald 
101fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
10236944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
103fcadd0caSmatthias.ringwald }
10436944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
105b502e1b0Smatthias.ringwald     packet_handler = handler;
1061e6aba47Smatthias.ringwald }
1071e6aba47Smatthias.ringwald 
10858de5610Smatthias.ringwald //  notify client/protocol handler
10958de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
11058de5610Smatthias.ringwald     if (channel->packet_handler) {
11158de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
11258de5610Smatthias.ringwald     } else {
11336944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
11458de5610Smatthias.ringwald     }
11558de5610Smatthias.ringwald }
11658de5610Smatthias.ringwald 
11758de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1189893b714Smatthias.ringwald     uint8_t event[21];
11958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
12058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12158de5610Smatthias.ringwald     event[2] = status;
12258de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
12358de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
12458de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
12558de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
12658de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1274c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1284c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
12958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13058de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13158de5610Smatthias.ringwald }
13258de5610Smatthias.ringwald 
13358de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
13458de5610Smatthias.ringwald     uint8_t event[4];
13558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
13658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13758de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
13858de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13958de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
14058de5610Smatthias.ringwald }
14158de5610Smatthias.ringwald 
14258de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
14358de5610Smatthias.ringwald     uint8_t event[16];
14458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
14558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14658de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
14758de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
14858de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
14958de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
15058de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
15158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1530af41d30Smatthias.ringwald }
154808a48abSmatthias.ringwald 
155*5842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
156*5842b6d9Smatthias.ringwald     uint8_t event[5];
157*5842b6d9Smatthias.ringwald     event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
158*5842b6d9Smatthias.ringwald     event[1] = sizeof(event) - 2;
159*5842b6d9Smatthias.ringwald     event[2] = status;
160*5842b6d9Smatthias.ringwald     bt_store_16(event, 3, psm);
161*5842b6d9Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
162*5842b6d9Smatthias.ringwald     (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
163*5842b6d9Smatthias.ringwald }
164*5842b6d9Smatthias.ringwald 
1656218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1666218e6f1Smatthias.ringwald     // track credits
1676218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1687b5fbe1fSmatthias.ringwald     // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1696218e6f1Smatthias.ringwald 
1706218e6f1Smatthias.ringwald     uint8_t event[5];
1716218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1726218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1736218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1746218e6f1Smatthias.ringwald     event[4] = credits;
1756218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1766218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1776218e6f1Smatthias.ringwald }
1786218e6f1Smatthias.ringwald 
179808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
180808a48abSmatthias.ringwald     new_credits_blocked = blocked;
181808a48abSmatthias.ringwald }
182808a48abSmatthias.ringwald 
18340d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
184808a48abSmatthias.ringwald 
185808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
186808a48abSmatthias.ringwald 
1878d371091Smatthias.ringwald     linked_item_t *it;
1888d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1898d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1908d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1910e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
1924c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
1938d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
1948d371091Smatthias.ringwald         }
1958d371091Smatthias.ringwald     }
1968d371091Smatthias.ringwald }
1978d371091Smatthias.ringwald 
198b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
199f62db1e3Smatthias.ringwald     linked_item_t *it;
200f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2018d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
202b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
203f62db1e3Smatthias.ringwald             return channel;
204f62db1e3Smatthias.ringwald         }
205f62db1e3Smatthias.ringwald     }
206f62db1e3Smatthias.ringwald     return NULL;
207f62db1e3Smatthias.ringwald }
208f62db1e3Smatthias.ringwald 
2096b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2106b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2116b1fde37Smatthias.ringwald     if (!channel) return 0;
2126b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2136b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2146b1fde37Smatthias.ringwald }
2156b1fde37Smatthias.ringwald 
21696cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
21796cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
21896cbd662Smatthias.ringwald     if (channel) {
21996cbd662Smatthias.ringwald         return channel->remote_mtu;
22096cbd662Smatthias.ringwald     }
22196cbd662Smatthias.ringwald     return 0;
22296cbd662Smatthias.ringwald }
22396cbd662Smatthias.ringwald 
22458de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
225b1d43497Smatthias.ringwald 
226b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
227b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
228b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
229b1d43497Smatthias.ringwald     }
230b1d43497Smatthias.ringwald 
2317b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
232b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
23358de5610Smatthias.ringwald     va_list argptr;
23458de5610Smatthias.ringwald     va_start(argptr, identifier);
235816c0598Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
23658de5610Smatthias.ringwald     va_end(argptr);
2377b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
238816c0598Smatthias.ringwald     return hci_send_acl_packet(acl_buffer, len);
23958de5610Smatthias.ringwald }
24058de5610Smatthias.ringwald 
241b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
242b1d43497Smatthias.ringwald     return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
243b1d43497Smatthias.ringwald }
2446218e6f1Smatthias.ringwald 
245b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
246b1d43497Smatthias.ringwald 
247b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
248b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
249808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2508ea03fa5Smatthias.ringwald     }
2516218e6f1Smatthias.ringwald 
25258de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
253b1d43497Smatthias.ringwald     if (!channel) {
254b1d43497Smatthias.ringwald         log_error("l2cap_send_internal no channel for cid %u\n", local_cid);
255b1d43497Smatthias.ringwald         return -1;   // TODO: define error
2566218e6f1Smatthias.ringwald     }
2576218e6f1Smatthias.ringwald 
258b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
259b1d43497Smatthias.ringwald         log_error("l2cap_send_internal cid %u, no credits!\n", local_cid);
260b1d43497Smatthias.ringwald         return -1;  // TODO: define error
261b1d43497Smatthias.ringwald     }
262b1d43497Smatthias.ringwald 
263b1d43497Smatthias.ringwald     --channel->packets_granted;
264b1d43497Smatthias.ringwald 
265b1d43497Smatthias.ringwald     log_debug("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
266b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
267b1d43497Smatthias.ringwald 
268b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
269b1d43497Smatthias.ringwald 
27058de5610Smatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
27158de5610Smatthias.ringwald     bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
27258de5610Smatthias.ringwald     // 2 - ACL length
27358de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
27458de5610Smatthias.ringwald     // 4 - L2CAP packet length
27558de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
27658de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
27758de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
27858de5610Smatthias.ringwald     // send
279b1d43497Smatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
28091b99603Smatthias.ringwald 
28191b99603Smatthias.ringwald     l2cap_hand_out_credits();
28291b99603Smatthias.ringwald 
2836218e6f1Smatthias.ringwald     return err;
28458de5610Smatthias.ringwald }
28558de5610Smatthias.ringwald 
286b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
287b1d43497Smatthias.ringwald 
288b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
289b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
290b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
291b1d43497Smatthias.ringwald     }
292b1d43497Smatthias.ringwald 
293b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
294b1d43497Smatthias.ringwald 
295b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
296b1d43497Smatthias.ringwald 
297b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
298b1d43497Smatthias.ringwald }
299b1d43497Smatthias.ringwald 
300b1d43497Smatthias.ringwald 
3018158c421Smatthias.ringwald // MARK: L2CAP_RUN
3022cd0be45Smatthias.ringwald // process outstanding signaling tasks
3032cd0be45Smatthias.ringwald void l2cap_run(void){
3042b83fb7dSmatthias.ringwald 
3052b83fb7dSmatthias.ringwald     // check pending signaling responses
3062b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
3072b83fb7dSmatthias.ringwald 
30802b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3092b83fb7dSmatthias.ringwald 
3102b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3112b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3122b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
3132b360848Smatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST
3142b83fb7dSmatthias.ringwald 
3152b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3162b360848Smatthias.ringwald             case CONNECTION_REQUEST:
3172b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
3182b360848Smatthias.ringwald                 break;
3192b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3202b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3212b83fb7dSmatthias.ringwald                 break;
3222b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3232b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3242b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3252b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3262b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3272b83fb7dSmatthias.ringwald                 } else {
3282b83fb7dSmatthias.ringwald                     // all other types are not supported
3292b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3302b83fb7dSmatthias.ringwald                 }
3312b83fb7dSmatthias.ringwald                 break;
3322b83fb7dSmatthias.ringwald             default:
3332b83fb7dSmatthias.ringwald                 // should not happen
3342b83fb7dSmatthias.ringwald                 break;
3352b83fb7dSmatthias.ringwald         }
3362b83fb7dSmatthias.ringwald 
3372b83fb7dSmatthias.ringwald         // remove first item
3382b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3392b83fb7dSmatthias.ringwald         int i;
3402b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
34176115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
3422b83fb7dSmatthias.ringwald         }
3432b83fb7dSmatthias.ringwald     }
3442b83fb7dSmatthias.ringwald 
345ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3462cd0be45Smatthias.ringwald     linked_item_t *it;
3472cd0be45Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
3482cd0be45Smatthias.ringwald 
34902b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
35002b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3512cd0be45Smatthias.ringwald 
3522cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
3536e6710ebSmatthias.ringwald 
3547b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
3556e6710ebSmatthias.ringwald 
35656081214Smatthias.ringwald 
3572cd0be45Smatthias.ringwald         switch (channel->state){
3582cd0be45Smatthias.ringwald 
35902b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
36064472d52Smatthias.ringwald                 // send connection request - set state first
36164472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
36202b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
3638f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
36402b22dc4Smatthias.ringwald                 break;
36502b22dc4Smatthias.ringwald 
366e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
367b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
368e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
369e7ff783cSmatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
370d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
371e7ff783cSmatthias.ringwald                 break;
372e7ff783cSmatthias.ringwald 
373552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
374fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
37573cf2b3dSmatthias.ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
3762a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
377552d92a1Smatthias.ringwald                 break;
378552d92a1Smatthias.ringwald 
3796fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
3806fdcc387Smatthias.ringwald                 // success, start l2cap handshake
381b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
3826fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
3832a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
3846fdcc387Smatthias.ringwald                 break;
3856fdcc387Smatthias.ringwald 
386fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
38773cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
38873cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
38973cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP;
390fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
391fa8473a4Smatthias.ringwald                 }
39273cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
39373cf2b3dSmatthias.ringwald                     channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
39473cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ;
395b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
396ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
397ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
398ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
399b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
400fa8473a4Smatthias.ringwald                 }
401fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
402552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
403552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
404552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
405fa8473a4Smatthias.ringwald                 }
406552d92a1Smatthias.ringwald                 break;
407552d92a1Smatthias.ringwald 
408e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
409b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
410e7ff783cSmatthias.ringwald                 l2cap_finialize_channel_close(channel);
411e7ff783cSmatthias.ringwald                 break;
412e7ff783cSmatthias.ringwald 
413e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
414b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4152cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4162a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4172cd0be45Smatthias.ringwald                 break;
4182cd0be45Smatthias.ringwald             default:
4192cd0be45Smatthias.ringwald                 break;
4202cd0be45Smatthias.ringwald         }
4212cd0be45Smatthias.ringwald     }
4222cd0be45Smatthias.ringwald }
4232cd0be45Smatthias.ringwald 
4244aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
425816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
426fa8c92f6Smatthias.ringwald }
427fa8c92f6Smatthias.ringwald 
4281e6aba47Smatthias.ringwald // open outgoing L2CAP channel
42915470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
43015470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4311e6aba47Smatthias.ringwald 
4321e6aba47Smatthias.ringwald     // alloc structure
433d3a9df87Smatthias.ringwald     l2cap_channel_t * chan = btstack_memory_l2cap_channel_get();
4342b360848Smatthias.ringwald     if (!chan) {
4352b360848Smatthias.ringwald         // emit error event
4362b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
4372b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
4382b360848Smatthias.ringwald         dummy_channel.psm = psm;
4392b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
4402b360848Smatthias.ringwald         return;
4412b360848Smatthias.ringwald     }
4421d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
4432985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
4442985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
4459775e25bSmatthias.ringwald     }
4469775e25bSmatthias.ringwald 
4471e6aba47Smatthias.ringwald     // fill in
4481e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4491e6aba47Smatthias.ringwald     chan->psm = psm;
4501e6aba47Smatthias.ringwald     chan->handle = 0;
4511e6aba47Smatthias.ringwald     chan->connection = connection;
4526b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4532784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
45415470d27Smatthias.ringwald     chan->local_mtu = mtu;
4556218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4566218e6f1Smatthias.ringwald 
4571e6aba47Smatthias.ringwald     // set initial state
45802b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
45973cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
460b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
461b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
4621e6aba47Smatthias.ringwald 
4631e6aba47Smatthias.ringwald     // add to connections list
4641e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
4651e6aba47Smatthias.ringwald 
46602b22dc4Smatthias.ringwald     l2cap_run();
46743625864Smatthias.ringwald }
46843625864Smatthias.ringwald 
469b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
470b35f641cSmatthias.ringwald     // find channel for local_cid
471b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
472f62db1e3Smatthias.ringwald     if (channel) {
473e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
474f62db1e3Smatthias.ringwald     }
4752cd0be45Smatthias.ringwald     // process
4762cd0be45Smatthias.ringwald     l2cap_run();
47743625864Smatthias.ringwald }
4781e6aba47Smatthias.ringwald 
479afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
48015ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
48115ec09bbSmatthias.ringwald     while (it->next){
48215ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
483b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
48402b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
485afde0c52Smatthias.ringwald                 // failure, forward error code
486afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
487afde0c52Smatthias.ringwald                 // discard channel
48815ec09bbSmatthias.ringwald                 it->next = it->next->next;
489d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
490afde0c52Smatthias.ringwald             }
49115ec09bbSmatthias.ringwald         } else {
49215ec09bbSmatthias.ringwald             it = it->next;
493afde0c52Smatthias.ringwald         }
494afde0c52Smatthias.ringwald     }
495afde0c52Smatthias.ringwald }
496afde0c52Smatthias.ringwald 
497afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
498afde0c52Smatthias.ringwald     linked_item_t *it;
499afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
500afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
501afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
50202b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
503b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5046fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
505afde0c52Smatthias.ringwald                 channel->handle = handle;
506b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
507afde0c52Smatthias.ringwald             }
508afde0c52Smatthias.ringwald         }
509afde0c52Smatthias.ringwald     }
5106fdcc387Smatthias.ringwald     // process
5116fdcc387Smatthias.ringwald     l2cap_run();
512afde0c52Smatthias.ringwald }
513b448a0e7Smatthias.ringwald 
514afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
515afde0c52Smatthias.ringwald 
516afde0c52Smatthias.ringwald     bd_addr_t address;
517afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5186e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
51936944dffSmatthias.ringwald     linked_item_t *it;
5202d00edd4Smatthias.ringwald     int hci_con_used;
521afde0c52Smatthias.ringwald 
522afde0c52Smatthias.ringwald     switch(packet[0]){
523afde0c52Smatthias.ringwald 
524afde0c52Smatthias.ringwald         // handle connection complete events
525afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
526afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
527afde0c52Smatthias.ringwald             if (packet[2] == 0){
528afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
529afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
530afde0c52Smatthias.ringwald             } else {
531afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
532afde0c52Smatthias.ringwald             }
533afde0c52Smatthias.ringwald             break;
534afde0c52Smatthias.ringwald 
535afde0c52Smatthias.ringwald         // handle successful create connection cancel command
536afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
537afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
538afde0c52Smatthias.ringwald                 if (packet[5] == 0){
539afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
540afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
541afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
54203cfbabcSmatthias.ringwald                 }
5431e6aba47Smatthias.ringwald             }
54439d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
54539d59809Smatthias.ringwald             break;
54639d59809Smatthias.ringwald 
54739d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
54839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
549afde0c52Smatthias.ringwald             break;
55027a923d0Smatthias.ringwald 
5511e6aba47Smatthias.ringwald         // handle disconnection complete events
552afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
55327a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
554afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
55515ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
55615ec09bbSmatthias.ringwald             while (it->next){
55739ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
55827a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
55915ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
5602060cf14Smatthias.ringwald                     it->next = it->next->next;
56115ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
562d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
56315ec09bbSmatthias.ringwald                 } else {
56415ec09bbSmatthias.ringwald                     it = it->next;
56527a923d0Smatthias.ringwald                 }
56627a923d0Smatthias.ringwald             }
567afde0c52Smatthias.ringwald             break;
568fcadd0caSmatthias.ringwald 
5696218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
57002b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
5718d371091Smatthias.ringwald             l2cap_hand_out_credits();
5726218e6f1Smatthias.ringwald             break;
5736218e6f1Smatthias.ringwald 
574ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
575afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
57636944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
57780ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
5782d00edd4Smatthias.ringwald             hci_con_used = 0;
579ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
580ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
581ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
5822d00edd4Smatthias.ringwald                     hci_con_used = 1;
583ee091cf1Smatthias.ringwald                 }
584ee091cf1Smatthias.ringwald             }
5852d00edd4Smatthias.ringwald             if (hci_con_used) break;
58632ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
5879edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
588afde0c52Smatthias.ringwald             break;
589ee091cf1Smatthias.ringwald 
5906e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
5916e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
5926e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
5936e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
5946e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
5956e6710ebSmatthias.ringwald                 }
5966e6710ebSmatthias.ringwald             }
5976e6710ebSmatthias.ringwald             break;
5986e6710ebSmatthias.ringwald 
599afde0c52Smatthias.ringwald         default:
600afde0c52Smatthias.ringwald             break;
601afde0c52Smatthias.ringwald     }
602afde0c52Smatthias.ringwald 
603afde0c52Smatthias.ringwald     // pass on
604b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
6051e6aba47Smatthias.ringwald }
6061e6aba47Smatthias.ringwald 
607afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
608b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
609e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
610e7ff783cSmatthias.ringwald     l2cap_run();
61184836b65Smatthias.ringwald }
61284836b65Smatthias.ringwald 
6132b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
6142b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
6152b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
6162b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
6172b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
6182b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
6192b360848Smatthias.ringwald         signaling_responses_pending++;
6202b360848Smatthias.ringwald         l2cap_run();
6212b360848Smatthias.ringwald     }
6222b360848Smatthias.ringwald }
6232b360848Smatthias.ringwald 
624b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
625645658c9Smatthias.ringwald 
6267b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
627645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
628645658c9Smatthias.ringwald     if (!service) {
629645658c9Smatthias.ringwald         // 0x0002 PSM not supported
6302b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
631645658c9Smatthias.ringwald         return;
632645658c9Smatthias.ringwald     }
633645658c9Smatthias.ringwald 
634645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
635645658c9Smatthias.ringwald     if (!hci_connection) {
6362b360848Smatthias.ringwald         //
6377d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
638645658c9Smatthias.ringwald         return;
639645658c9Smatthias.ringwald     }
640645658c9Smatthias.ringwald     // alloc structure
6417b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
642d3a9df87Smatthias.ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
6432b360848Smatthias.ringwald     if (!channel){
6442b360848Smatthias.ringwald         // 0x0004 No resources available
6452b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
6462b360848Smatthias.ringwald         return;
6472b360848Smatthias.ringwald     }
648645658c9Smatthias.ringwald 
649645658c9Smatthias.ringwald     // fill in
650169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
651169f8b28Smatthias.ringwald     channel->psm = psm;
652169f8b28Smatthias.ringwald     channel->handle = handle;
653169f8b28Smatthias.ringwald     channel->connection = service->connection;
654f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
655b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
656b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
657fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6580a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
659761b0451Smatthias.ringwald     channel->packets_granted = 0;
660b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
661645658c9Smatthias.ringwald 
6621d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
6632985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
6642985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
6659775e25bSmatthias.ringwald     }
6669775e25bSmatthias.ringwald 
667645658c9Smatthias.ringwald     // set initial state
668e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
66973cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
670e405ae81Smatthias.ringwald 
671645658c9Smatthias.ringwald     // add to connections list
672169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
673645658c9Smatthias.ringwald 
674e405ae81Smatthias.ringwald     // emit incoming connection request
675e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
676e405ae81Smatthias.ringwald }
677645658c9Smatthias.ringwald 
678b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
679b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
680e405ae81Smatthias.ringwald     if (!channel) {
6817d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
682e405ae81Smatthias.ringwald         return;
683e405ae81Smatthias.ringwald     }
684e405ae81Smatthias.ringwald 
685552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
686e405ae81Smatthias.ringwald 
687552d92a1Smatthias.ringwald     // process
688552d92a1Smatthias.ringwald     l2cap_run();
689e405ae81Smatthias.ringwald }
690645658c9Smatthias.ringwald 
691b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
692b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
693e405ae81Smatthias.ringwald     if (!channel) {
6947d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
695e405ae81Smatthias.ringwald         return;
696e405ae81Smatthias.ringwald     }
697e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
698e7ff783cSmatthias.ringwald     channel->reason = reason;
699e7ff783cSmatthias.ringwald     l2cap_run();
700645658c9Smatthias.ringwald }
701645658c9Smatthias.ringwald 
7022784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
703b1988dceSmatthias.ringwald 
704b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
705b1988dceSmatthias.ringwald 
7062784b77dSmatthias.ringwald     // accept the other's configuration options
7073de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
7083de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7093de7c0caSmatthias.ringwald     while (pos < end_pos){
7101dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7111dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7121dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7131dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7141dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
7157b5fbe1fSmatthias.ringwald             // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7161dc511deSmatthias.ringwald         }
7171dc511deSmatthias.ringwald         pos += length;
7181dc511deSmatthias.ringwald     }
7192784b77dSmatthias.ringwald }
7202784b77dSmatthias.ringwald 
721fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
7227b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
72373cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
72473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
725fa8473a4Smatthias.ringwald     return 1;
726fa8473a4Smatthias.ringwald }
727fa8473a4Smatthias.ringwald 
728fa8473a4Smatthias.ringwald 
72900d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7301e6aba47Smatthias.ringwald 
73100d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
73200d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
73338e5900eSmatthias.ringwald     uint16_t result = 0;
7341e6aba47Smatthias.ringwald 
735*5842b6d9Smatthias.ringwald     log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
736b35f641cSmatthias.ringwald 
7379a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7389a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7399a011532Smatthias.ringwald         switch (channel->state){
740fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7419a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7422b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7439a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7449a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7459a011532Smatthias.ringwald                 break;
7469a011532Smatthias.ringwald 
7479a011532Smatthias.ringwald             default:
7489a011532Smatthias.ringwald                 // ignore in other states
7499a011532Smatthias.ringwald                 break;
7509a011532Smatthias.ringwald         }
7519a011532Smatthias.ringwald         return;
7529a011532Smatthias.ringwald     }
7539a011532Smatthias.ringwald 
75456081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
7551e6aba47Smatthias.ringwald     switch (channel->state) {
7561e6aba47Smatthias.ringwald 
7571e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7581e6aba47Smatthias.ringwald             switch (code){
7591e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
76000d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
76138e5900eSmatthias.ringwald                     switch (result) {
76238e5900eSmatthias.ringwald                         case 0:
763169f8b28Smatthias.ringwald                             // successful connection
76400d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
765fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
76673cf2b3dSmatthias.ringwald                             channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ;
76738e5900eSmatthias.ringwald                             break;
76838e5900eSmatthias.ringwald                         case 1:
76938e5900eSmatthias.ringwald                             // connection pending. get some coffee
77038e5900eSmatthias.ringwald                             break;
77138e5900eSmatthias.ringwald                         default:
772eb920dbeSmatthias.ringwald                             // channel closed
773eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
774eb920dbeSmatthias.ringwald 
775f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
77638e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
777eb920dbeSmatthias.ringwald 
778eb920dbeSmatthias.ringwald                             // drop link key if security block
779eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
780eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
781eb920dbeSmatthias.ringwald                             }
782eb920dbeSmatthias.ringwald 
783eb920dbeSmatthias.ringwald                             // discard channel
784eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
785d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
78638e5900eSmatthias.ringwald                             break;
7871e6aba47Smatthias.ringwald                     }
7881e6aba47Smatthias.ringwald                     break;
78938e5900eSmatthias.ringwald 
79038e5900eSmatthias.ringwald                 default:
7911e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
79238e5900eSmatthias.ringwald                     break;
7931e6aba47Smatthias.ringwald             }
7941e6aba47Smatthias.ringwald             break;
7951e6aba47Smatthias.ringwald 
796fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
797ae280e73Smatthias.ringwald             switch (code) {
798ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
79973cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ;
80073cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP;
801ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
802ae280e73Smatthias.ringwald                     break;
8031e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
80473cf2b3dSmatthias.ringwald                     channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP;
8055a67bd4aSmatthias.ringwald                     break;
8065a67bd4aSmatthias.ringwald                 default:
8075a67bd4aSmatthias.ringwald                     break;
8081e6aba47Smatthias.ringwald             }
809fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
810fa8473a4Smatthias.ringwald                 // for open:
8115a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
812fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8136218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
814c8e4258aSmatthias.ringwald             }
815c8e4258aSmatthias.ringwald             break;
816f62db1e3Smatthias.ringwald 
817f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
818f62db1e3Smatthias.ringwald             switch (code) {
819f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
82027a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
82127a923d0Smatthias.ringwald                     break;
8225a67bd4aSmatthias.ringwald                 default:
8235a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8245a67bd4aSmatthias.ringwald                     break;
82527a923d0Smatthias.ringwald             }
82627a923d0Smatthias.ringwald             break;
82784836b65Smatthias.ringwald 
82884836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
82984836b65Smatthias.ringwald             // @TODO handle incoming requests
83084836b65Smatthias.ringwald             break;
83184836b65Smatthias.ringwald 
83284836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
83384836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
83484836b65Smatthias.ringwald             break;
83510642e45Smatthias.ringwald         default:
83610642e45Smatthias.ringwald             break;
83727a923d0Smatthias.ringwald     }
8387b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
83927a923d0Smatthias.ringwald }
84027a923d0Smatthias.ringwald 
84100d93d79Smatthias.ringwald 
84200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
84300d93d79Smatthias.ringwald 
84400d93d79Smatthias.ringwald     // get code, signalind identifier and command len
84500d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
84600d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
84700d93d79Smatthias.ringwald 
84800d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
84900d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
85000d93d79Smatthias.ringwald         return;
85100d93d79Smatthias.ringwald     }
85200d93d79Smatthias.ringwald 
85300d93d79Smatthias.ringwald     // general commands without an assigned channel
85400d93d79Smatthias.ringwald     switch(code) {
85500d93d79Smatthias.ringwald 
85600d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
85700d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
85800d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
85900d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8602b83fb7dSmatthias.ringwald             return;
86100d93d79Smatthias.ringwald         }
86200d93d79Smatthias.ringwald 
8632b360848Smatthias.ringwald         case ECHO_REQUEST:
8642b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
8652b83fb7dSmatthias.ringwald             return;
86600d93d79Smatthias.ringwald 
86700d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
86800d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
8692b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
8702b83fb7dSmatthias.ringwald             return;
87100d93d79Smatthias.ringwald         }
87200d93d79Smatthias.ringwald 
87300d93d79Smatthias.ringwald         default:
87400d93d79Smatthias.ringwald             break;
87500d93d79Smatthias.ringwald     }
87600d93d79Smatthias.ringwald 
87700d93d79Smatthias.ringwald 
87800d93d79Smatthias.ringwald     // Get potential destination CID
87900d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
88000d93d79Smatthias.ringwald 
88100d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
88200d93d79Smatthias.ringwald     linked_item_t *it;
88300d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
88400d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
88500d93d79Smatthias.ringwald         if (channel->handle == handle) {
88600d93d79Smatthias.ringwald             if (code & 1) {
887b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
888b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
88900d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8904e32727eSmatthias.ringwald                     break;
89100d93d79Smatthias.ringwald                 }
89200d93d79Smatthias.ringwald             } else {
893b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
89400d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
89500d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
8964e32727eSmatthias.ringwald                     break;
89700d93d79Smatthias.ringwald                 }
89800d93d79Smatthias.ringwald             }
89900d93d79Smatthias.ringwald         }
90000d93d79Smatthias.ringwald     }
90100d93d79Smatthias.ringwald }
90200d93d79Smatthias.ringwald 
90300d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
90400d93d79Smatthias.ringwald 
90500d93d79Smatthias.ringwald     // Get Channel ID
90600d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
90700d93d79Smatthias.ringwald 
90800d93d79Smatthias.ringwald     // Signaling Packet?
90900d93d79Smatthias.ringwald     if (channel_id == 1) {
91000d93d79Smatthias.ringwald 
91100d93d79Smatthias.ringwald         // Get Connection
91200d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
91300d93d79Smatthias.ringwald 
91400d93d79Smatthias.ringwald         uint16_t command_offset = 8;
91500d93d79Smatthias.ringwald         while (command_offset < size) {
91600d93d79Smatthias.ringwald 
91700d93d79Smatthias.ringwald             // handle signaling commands
91800d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
91900d93d79Smatthias.ringwald 
92000d93d79Smatthias.ringwald             // increment command_offset
92100d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
92200d93d79Smatthias.ringwald         }
92364472d52Smatthias.ringwald 
92464472d52Smatthias.ringwald         l2cap_run();
92564472d52Smatthias.ringwald 
92600d93d79Smatthias.ringwald         return;
92700d93d79Smatthias.ringwald     }
92800d93d79Smatthias.ringwald 
92900d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
93000d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
93100d93d79Smatthias.ringwald     if (channel) {
93258de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
93300d93d79Smatthias.ringwald     }
93400d93d79Smatthias.ringwald }
93500d93d79Smatthias.ringwald 
9362718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9372718e2e7Smatthias.ringwald     switch (packet_type) {
9382718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9392718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9402718e2e7Smatthias.ringwald             break;
9412718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9422718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9432718e2e7Smatthias.ringwald             break;
9442718e2e7Smatthias.ringwald         default:
9452718e2e7Smatthias.ringwald             break;
9462718e2e7Smatthias.ringwald     }
9472718e2e7Smatthias.ringwald }
94800d93d79Smatthias.ringwald 
94915ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
95027a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
951f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
952f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
953f62db1e3Smatthias.ringwald     // discard channel
954f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
955d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
956c8e4258aSmatthias.ringwald }
9571e6aba47Smatthias.ringwald 
9589d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
959c52bf64dSmatthias.ringwald     linked_item_t *it;
9609d9bbc01Smatthias.ringwald 
9619d9bbc01Smatthias.ringwald     // close open channels
9629d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9639d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9649d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9659d9bbc01Smatthias.ringwald             return service;
9669d9bbc01Smatthias.ringwald         };
9679d9bbc01Smatthias.ringwald     }
9689d9bbc01Smatthias.ringwald     return NULL;
9699d9bbc01Smatthias.ringwald }
9709d9bbc01Smatthias.ringwald 
97136944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
9724bb582b6Smatthias.ringwald     // check for alread registered psm
9734bb582b6Smatthias.ringwald     // TODO: emit error event
9749d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
975277abc2cSmatthias.ringwald     if (service) {
976277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
977*5842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_CHANNEL_ALREADY_REGISTERED, psm);
978277abc2cSmatthias.ringwald         return;
979277abc2cSmatthias.ringwald     }
9809d9bbc01Smatthias.ringwald 
9814bb582b6Smatthias.ringwald     // alloc structure
9824bb582b6Smatthias.ringwald     // TODO: emit error event
983d3a9df87Smatthias.ringwald     service = btstack_memory_l2cap_service_get();
984277abc2cSmatthias.ringwald     if (!service) {
985277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
986*5842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
987277abc2cSmatthias.ringwald         return;
988277abc2cSmatthias.ringwald     }
9899d9bbc01Smatthias.ringwald 
9909d9bbc01Smatthias.ringwald     // fill in
9919d9bbc01Smatthias.ringwald     service->psm = psm;
9929d9bbc01Smatthias.ringwald     service->mtu = mtu;
9939d9bbc01Smatthias.ringwald     service->connection = connection;
994d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
9959d9bbc01Smatthias.ringwald 
9969d9bbc01Smatthias.ringwald     // add to services list
9979d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
998c0e866bfSmatthias.ringwald 
999c0e866bfSmatthias.ringwald     // enable page scan
1000c0e866bfSmatthias.ringwald     hci_connectable_control(1);
1001*5842b6d9Smatthias.ringwald 
1002*5842b6d9Smatthias.ringwald     // done
1003*5842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
10049d9bbc01Smatthias.ringwald }
10059d9bbc01Smatthias.ringwald 
100636944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
10079d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1008037d6e48Smatthias.ringwald     if (!service) return;
10099d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1010d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1011c0e866bfSmatthias.ringwald 
1012c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1013c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1014c0e866bfSmatthias.ringwald     hci_connectable_control(0);
10159d9bbc01Smatthias.ringwald }
10169d9bbc01Smatthias.ringwald 
10179d9bbc01Smatthias.ringwald //
101836944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
10199d9bbc01Smatthias.ringwald     linked_item_t *it;
10209d9bbc01Smatthias.ringwald 
10213a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1022c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1023c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1024c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1025c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1026cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1027c52bf64dSmatthias.ringwald         }
1028c52bf64dSmatthias.ringwald     }
10299d9bbc01Smatthias.ringwald 
1030645658c9Smatthias.ringwald     // unregister services
103169025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
103269025de8Smatthias.ringwald     while (it->next) {
103369025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1034645658c9Smatthias.ringwald         if (service->connection == connection){
103569025de8Smatthias.ringwald             it->next = it->next->next;
1036d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
10378149d2c7Smatthias.ringwald         } else {
10388149d2c7Smatthias.ringwald             it = it->next;
1039645658c9Smatthias.ringwald         }
1040645658c9Smatthias.ringwald     }
1041cb8eb7e6Smatthias.ringwald 
1042cb8eb7e6Smatthias.ringwald     // process
1043cb8eb7e6Smatthias.ringwald     l2cap_run();
1044c52bf64dSmatthias.ringwald }
1045