xref: /btstack/src/l2cap.c (revision 28ca2b4607e125f45dc135e6777e2b6822e52f41)
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 
1555842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
1565842b6d9Smatthias.ringwald     uint8_t event[5];
1575842b6d9Smatthias.ringwald     event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
1585842b6d9Smatthias.ringwald     event[1] = sizeof(event) - 2;
1595842b6d9Smatthias.ringwald     event[2] = status;
1605842b6d9Smatthias.ringwald     bt_store_16(event, 3, psm);
1615842b6d9Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1625842b6d9Smatthias.ringwald     (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
1635842b6d9Smatthias.ringwald }
1645842b6d9Smatthias.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 
300*28ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
301*28ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
302*28ca2b46S[email protected] }
303*28ca2b46S[email protected] 
304*28ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
305*28ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
306*28ca2b46S[email protected] }
307*28ca2b46S[email protected] 
308*28ca2b46S[email protected] 
309b1d43497Smatthias.ringwald 
3108158c421Smatthias.ringwald // MARK: L2CAP_RUN
3112cd0be45Smatthias.ringwald // process outstanding signaling tasks
3122cd0be45Smatthias.ringwald void l2cap_run(void){
3132b83fb7dSmatthias.ringwald 
3142b83fb7dSmatthias.ringwald     // check pending signaling responses
3152b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
3162b83fb7dSmatthias.ringwald 
31702b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3182b83fb7dSmatthias.ringwald 
3192b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3202b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3212b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
3222b360848Smatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST
3232b83fb7dSmatthias.ringwald 
3242b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3252b360848Smatthias.ringwald             case CONNECTION_REQUEST:
3262b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
3272b360848Smatthias.ringwald                 break;
3282b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3292b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3302b83fb7dSmatthias.ringwald                 break;
3312b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3322b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3332b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3342b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3352b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3362b83fb7dSmatthias.ringwald                 } else {
3372b83fb7dSmatthias.ringwald                     // all other types are not supported
3382b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3392b83fb7dSmatthias.ringwald                 }
3402b83fb7dSmatthias.ringwald                 break;
3412b83fb7dSmatthias.ringwald             default:
3422b83fb7dSmatthias.ringwald                 // should not happen
3432b83fb7dSmatthias.ringwald                 break;
3442b83fb7dSmatthias.ringwald         }
3452b83fb7dSmatthias.ringwald 
3462b83fb7dSmatthias.ringwald         // remove first item
3472b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3482b83fb7dSmatthias.ringwald         int i;
3492b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
35076115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
3512b83fb7dSmatthias.ringwald         }
3522b83fb7dSmatthias.ringwald     }
3532b83fb7dSmatthias.ringwald 
354ae280e73Smatthias.ringwald     uint8_t  config_options[4];
3552cd0be45Smatthias.ringwald     linked_item_t *it;
356756102d3Smatthias.ringwald     linked_item_t *next;
357756102d3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = next){
358756102d3Smatthias.ringwald         next = it->next;    // cache next item as current item might get freed
3592cd0be45Smatthias.ringwald 
36002b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
36102b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3622cd0be45Smatthias.ringwald 
3632cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
3646e6710ebSmatthias.ringwald 
3657b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
3666e6710ebSmatthias.ringwald 
36756081214Smatthias.ringwald 
3682cd0be45Smatthias.ringwald         switch (channel->state){
3692cd0be45Smatthias.ringwald 
37002b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
37164472d52Smatthias.ringwald                 // send connection request - set state first
37264472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
37302b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
3748f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
37502b22dc4Smatthias.ringwald                 break;
37602b22dc4Smatthias.ringwald 
377e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
378b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
379e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
380756102d3Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list
381d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
382e7ff783cSmatthias.ringwald                 break;
383e7ff783cSmatthias.ringwald 
384552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
385fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
386*28ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
3872a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
388552d92a1Smatthias.ringwald                 break;
389552d92a1Smatthias.ringwald 
3906fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
3916fdcc387Smatthias.ringwald                 // success, start l2cap handshake
392b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
3936fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
3942a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
3956fdcc387Smatthias.ringwald                 break;
3966fdcc387Smatthias.ringwald 
397fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
39873cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
399*28ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
400*28ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
401fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
402fa8473a4Smatthias.ringwald                 }
40373cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
404*28ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
405*28ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
406b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
407ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
408ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
409ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
410b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
411fa8473a4Smatthias.ringwald                 }
412fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
413552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
414552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
415552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
416fa8473a4Smatthias.ringwald                 }
417552d92a1Smatthias.ringwald                 break;
418552d92a1Smatthias.ringwald 
419e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
420b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
421756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
422e7ff783cSmatthias.ringwald                 break;
423e7ff783cSmatthias.ringwald 
424e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
425b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4262cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4272a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4282cd0be45Smatthias.ringwald                 break;
4292cd0be45Smatthias.ringwald             default:
4302cd0be45Smatthias.ringwald                 break;
4312cd0be45Smatthias.ringwald         }
4322cd0be45Smatthias.ringwald     }
4332cd0be45Smatthias.ringwald }
4342cd0be45Smatthias.ringwald 
4354aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
436816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
437fa8c92f6Smatthias.ringwald }
438fa8c92f6Smatthias.ringwald 
4391e6aba47Smatthias.ringwald // open outgoing L2CAP channel
44015470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
44115470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4421e6aba47Smatthias.ringwald 
4431e6aba47Smatthias.ringwald     // alloc structure
444*28ca2b46S[email protected]     l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
4452b360848Smatthias.ringwald     if (!chan) {
4462b360848Smatthias.ringwald         // emit error event
4472b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
4482b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
4492b360848Smatthias.ringwald         dummy_channel.psm = psm;
4502b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
4512b360848Smatthias.ringwald         return;
4522b360848Smatthias.ringwald     }
4531d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
4542985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
4552985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
4569775e25bSmatthias.ringwald     }
4579775e25bSmatthias.ringwald 
4581e6aba47Smatthias.ringwald     // fill in
4591e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
4601e6aba47Smatthias.ringwald     chan->psm = psm;
4611e6aba47Smatthias.ringwald     chan->handle = 0;
4621e6aba47Smatthias.ringwald     chan->connection = connection;
4636b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
4642784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
46515470d27Smatthias.ringwald     chan->local_mtu = mtu;
4666218e6f1Smatthias.ringwald     chan->packets_granted = 0;
4676218e6f1Smatthias.ringwald 
4681e6aba47Smatthias.ringwald     // set initial state
46902b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
47073cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
471b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
472b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
4731e6aba47Smatthias.ringwald 
4741e6aba47Smatthias.ringwald     // add to connections list
4751e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
4761e6aba47Smatthias.ringwald 
47702b22dc4Smatthias.ringwald     l2cap_run();
47843625864Smatthias.ringwald }
47943625864Smatthias.ringwald 
480b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
481b35f641cSmatthias.ringwald     // find channel for local_cid
482b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
483f62db1e3Smatthias.ringwald     if (channel) {
484e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
485f62db1e3Smatthias.ringwald     }
4862cd0be45Smatthias.ringwald     // process
4872cd0be45Smatthias.ringwald     l2cap_run();
48843625864Smatthias.ringwald }
4891e6aba47Smatthias.ringwald 
490afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
49115ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
49215ec09bbSmatthias.ringwald     while (it->next){
49315ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
494b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
49502b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
496afde0c52Smatthias.ringwald                 // failure, forward error code
497afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
498afde0c52Smatthias.ringwald                 // discard channel
49915ec09bbSmatthias.ringwald                 it->next = it->next->next;
500d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
501afde0c52Smatthias.ringwald             }
50215ec09bbSmatthias.ringwald         } else {
50315ec09bbSmatthias.ringwald             it = it->next;
504afde0c52Smatthias.ringwald         }
505afde0c52Smatthias.ringwald     }
506afde0c52Smatthias.ringwald }
507afde0c52Smatthias.ringwald 
508afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
509afde0c52Smatthias.ringwald     linked_item_t *it;
510afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
511afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
512afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
51302b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
514b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5156fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
516afde0c52Smatthias.ringwald                 channel->handle = handle;
517b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
518afde0c52Smatthias.ringwald             }
519afde0c52Smatthias.ringwald         }
520afde0c52Smatthias.ringwald     }
5216fdcc387Smatthias.ringwald     // process
5226fdcc387Smatthias.ringwald     l2cap_run();
523afde0c52Smatthias.ringwald }
524b448a0e7Smatthias.ringwald 
525afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
526afde0c52Smatthias.ringwald 
527afde0c52Smatthias.ringwald     bd_addr_t address;
528afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5296e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
53036944dffSmatthias.ringwald     linked_item_t *it;
5312d00edd4Smatthias.ringwald     int hci_con_used;
532afde0c52Smatthias.ringwald 
533afde0c52Smatthias.ringwald     switch(packet[0]){
534afde0c52Smatthias.ringwald 
535afde0c52Smatthias.ringwald         // handle connection complete events
536afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
537afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
538afde0c52Smatthias.ringwald             if (packet[2] == 0){
539afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
540afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
541afde0c52Smatthias.ringwald             } else {
542afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
543afde0c52Smatthias.ringwald             }
544afde0c52Smatthias.ringwald             break;
545afde0c52Smatthias.ringwald 
546afde0c52Smatthias.ringwald         // handle successful create connection cancel command
547afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
548afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
549afde0c52Smatthias.ringwald                 if (packet[5] == 0){
550afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
551afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
552afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
55303cfbabcSmatthias.ringwald                 }
5541e6aba47Smatthias.ringwald             }
55539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
55639d59809Smatthias.ringwald             break;
55739d59809Smatthias.ringwald 
55839d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
55939d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
560afde0c52Smatthias.ringwald             break;
56127a923d0Smatthias.ringwald 
5621e6aba47Smatthias.ringwald         // handle disconnection complete events
563afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
56427a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
565afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
56615ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
56715ec09bbSmatthias.ringwald             while (it->next){
56839ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
56927a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
57015ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
5712060cf14Smatthias.ringwald                     it->next = it->next->next;
57215ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
573d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
57415ec09bbSmatthias.ringwald                 } else {
57515ec09bbSmatthias.ringwald                     it = it->next;
57627a923d0Smatthias.ringwald                 }
57727a923d0Smatthias.ringwald             }
578afde0c52Smatthias.ringwald             break;
579fcadd0caSmatthias.ringwald 
5806218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
58102b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
5828d371091Smatthias.ringwald             l2cap_hand_out_credits();
5836218e6f1Smatthias.ringwald             break;
5846218e6f1Smatthias.ringwald 
585ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
586afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
58736944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
58880ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
5892d00edd4Smatthias.ringwald             hci_con_used = 0;
590ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
591ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
592ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
5932d00edd4Smatthias.ringwald                     hci_con_used = 1;
594ee091cf1Smatthias.ringwald                 }
595ee091cf1Smatthias.ringwald             }
5962d00edd4Smatthias.ringwald             if (hci_con_used) break;
59732ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
5989edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
599afde0c52Smatthias.ringwald             break;
600ee091cf1Smatthias.ringwald 
6016e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
6026e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
6036e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
6046e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
6056e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
6066e6710ebSmatthias.ringwald                 }
6076e6710ebSmatthias.ringwald             }
6086e6710ebSmatthias.ringwald             break;
6096e6710ebSmatthias.ringwald 
610afde0c52Smatthias.ringwald         default:
611afde0c52Smatthias.ringwald             break;
612afde0c52Smatthias.ringwald     }
613afde0c52Smatthias.ringwald 
614afde0c52Smatthias.ringwald     // pass on
615b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
6161e6aba47Smatthias.ringwald }
6171e6aba47Smatthias.ringwald 
618afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
619b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
620e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
621e7ff783cSmatthias.ringwald     l2cap_run();
62284836b65Smatthias.ringwald }
62384836b65Smatthias.ringwald 
6242b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
6252b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
6262b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
6272b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
6282b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
6292b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
6302b360848Smatthias.ringwald         signaling_responses_pending++;
6312b360848Smatthias.ringwald         l2cap_run();
6322b360848Smatthias.ringwald     }
6332b360848Smatthias.ringwald }
6342b360848Smatthias.ringwald 
635b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
636645658c9Smatthias.ringwald 
6377b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
638645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
639645658c9Smatthias.ringwald     if (!service) {
640645658c9Smatthias.ringwald         // 0x0002 PSM not supported
6412b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
642645658c9Smatthias.ringwald         return;
643645658c9Smatthias.ringwald     }
644645658c9Smatthias.ringwald 
645645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
646645658c9Smatthias.ringwald     if (!hci_connection) {
6472b360848Smatthias.ringwald         //
6487d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
649645658c9Smatthias.ringwald         return;
650645658c9Smatthias.ringwald     }
651645658c9Smatthias.ringwald     // alloc structure
6527b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
653*28ca2b46S[email protected]     l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
6542b360848Smatthias.ringwald     if (!channel){
6552b360848Smatthias.ringwald         // 0x0004 No resources available
6562b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
6572b360848Smatthias.ringwald         return;
6582b360848Smatthias.ringwald     }
659645658c9Smatthias.ringwald 
660645658c9Smatthias.ringwald     // fill in
661169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
662169f8b28Smatthias.ringwald     channel->psm = psm;
663169f8b28Smatthias.ringwald     channel->handle = handle;
664169f8b28Smatthias.ringwald     channel->connection = service->connection;
665f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
666b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
667b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
668fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
6690a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
670761b0451Smatthias.ringwald     channel->packets_granted = 0;
671b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
672645658c9Smatthias.ringwald 
6731d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
6742985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
6752985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
6769775e25bSmatthias.ringwald     }
6779775e25bSmatthias.ringwald 
678645658c9Smatthias.ringwald     // set initial state
679e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
68073cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
681e405ae81Smatthias.ringwald 
682645658c9Smatthias.ringwald     // add to connections list
683169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
684645658c9Smatthias.ringwald 
685e405ae81Smatthias.ringwald     // emit incoming connection request
686e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
687e405ae81Smatthias.ringwald }
688645658c9Smatthias.ringwald 
689b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
690b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
691e405ae81Smatthias.ringwald     if (!channel) {
6927d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
693e405ae81Smatthias.ringwald         return;
694e405ae81Smatthias.ringwald     }
695e405ae81Smatthias.ringwald 
696552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
697e405ae81Smatthias.ringwald 
698552d92a1Smatthias.ringwald     // process
699552d92a1Smatthias.ringwald     l2cap_run();
700e405ae81Smatthias.ringwald }
701645658c9Smatthias.ringwald 
702b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
703b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
704e405ae81Smatthias.ringwald     if (!channel) {
7057d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
706e405ae81Smatthias.ringwald         return;
707e405ae81Smatthias.ringwald     }
708e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
709e7ff783cSmatthias.ringwald     channel->reason = reason;
710e7ff783cSmatthias.ringwald     l2cap_run();
711645658c9Smatthias.ringwald }
712645658c9Smatthias.ringwald 
7132784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
714b1988dceSmatthias.ringwald 
715b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
716b1988dceSmatthias.ringwald 
7172784b77dSmatthias.ringwald     // accept the other's configuration options
7183de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
7193de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7203de7c0caSmatthias.ringwald     while (pos < end_pos){
7211dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7221dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7231dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7241dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7251dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
7267b5fbe1fSmatthias.ringwald             // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7271dc511deSmatthias.ringwald         }
7281dc511deSmatthias.ringwald         pos += length;
7291dc511deSmatthias.ringwald     }
7302784b77dSmatthias.ringwald }
7312784b77dSmatthias.ringwald 
732fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
7337b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
73473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
73573cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
736fa8473a4Smatthias.ringwald     return 1;
737fa8473a4Smatthias.ringwald }
738fa8473a4Smatthias.ringwald 
739fa8473a4Smatthias.ringwald 
74000d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7411e6aba47Smatthias.ringwald 
74200d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
74300d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
74438e5900eSmatthias.ringwald     uint16_t result = 0;
7451e6aba47Smatthias.ringwald 
7465842b6d9Smatthias.ringwald     log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
747b35f641cSmatthias.ringwald 
7489a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
7499a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
7509a011532Smatthias.ringwald         switch (channel->state){
751fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
7529a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
7532b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
7549a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
7559a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
7569a011532Smatthias.ringwald                 break;
7579a011532Smatthias.ringwald 
7589a011532Smatthias.ringwald             default:
7599a011532Smatthias.ringwald                 // ignore in other states
7609a011532Smatthias.ringwald                 break;
7619a011532Smatthias.ringwald         }
7629a011532Smatthias.ringwald         return;
7639a011532Smatthias.ringwald     }
7649a011532Smatthias.ringwald 
76556081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
7661e6aba47Smatthias.ringwald     switch (channel->state) {
7671e6aba47Smatthias.ringwald 
7681e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
7691e6aba47Smatthias.ringwald             switch (code){
7701e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
77100d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
77238e5900eSmatthias.ringwald                     switch (result) {
77338e5900eSmatthias.ringwald                         case 0:
774169f8b28Smatthias.ringwald                             // successful connection
77500d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
776fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
777*28ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
77838e5900eSmatthias.ringwald                             break;
77938e5900eSmatthias.ringwald                         case 1:
78038e5900eSmatthias.ringwald                             // connection pending. get some coffee
78138e5900eSmatthias.ringwald                             break;
78238e5900eSmatthias.ringwald                         default:
783eb920dbeSmatthias.ringwald                             // channel closed
784eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
785eb920dbeSmatthias.ringwald 
786f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
78738e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
788eb920dbeSmatthias.ringwald 
789eb920dbeSmatthias.ringwald                             // drop link key if security block
790eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
791eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
792eb920dbeSmatthias.ringwald                             }
793eb920dbeSmatthias.ringwald 
794eb920dbeSmatthias.ringwald                             // discard channel
795eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
796d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
79738e5900eSmatthias.ringwald                             break;
7981e6aba47Smatthias.ringwald                     }
7991e6aba47Smatthias.ringwald                     break;
80038e5900eSmatthias.ringwald 
80138e5900eSmatthias.ringwald                 default:
8021e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
80338e5900eSmatthias.ringwald                     break;
8041e6aba47Smatthias.ringwald             }
8051e6aba47Smatthias.ringwald             break;
8061e6aba47Smatthias.ringwald 
807fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
808ae280e73Smatthias.ringwald             switch (code) {
809ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
810*28ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
811*28ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
812ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
813ae280e73Smatthias.ringwald                     break;
8141e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
815*28ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
8165a67bd4aSmatthias.ringwald                     break;
8175a67bd4aSmatthias.ringwald                 default:
8185a67bd4aSmatthias.ringwald                     break;
8191e6aba47Smatthias.ringwald             }
820fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
821fa8473a4Smatthias.ringwald                 // for open:
8225a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
823fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8246218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
825c8e4258aSmatthias.ringwald             }
826c8e4258aSmatthias.ringwald             break;
827f62db1e3Smatthias.ringwald 
828f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
829f62db1e3Smatthias.ringwald             switch (code) {
830f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
83127a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
83227a923d0Smatthias.ringwald                     break;
8335a67bd4aSmatthias.ringwald                 default:
8345a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8355a67bd4aSmatthias.ringwald                     break;
83627a923d0Smatthias.ringwald             }
83727a923d0Smatthias.ringwald             break;
83884836b65Smatthias.ringwald 
83984836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
84084836b65Smatthias.ringwald             // @TODO handle incoming requests
84184836b65Smatthias.ringwald             break;
84284836b65Smatthias.ringwald 
84384836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
84484836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
84584836b65Smatthias.ringwald             break;
84610642e45Smatthias.ringwald         default:
84710642e45Smatthias.ringwald             break;
84827a923d0Smatthias.ringwald     }
8497b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
85027a923d0Smatthias.ringwald }
85127a923d0Smatthias.ringwald 
85200d93d79Smatthias.ringwald 
85300d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
85400d93d79Smatthias.ringwald 
85500d93d79Smatthias.ringwald     // get code, signalind identifier and command len
85600d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
85700d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
85800d93d79Smatthias.ringwald 
85900d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
86000d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
86100d93d79Smatthias.ringwald         return;
86200d93d79Smatthias.ringwald     }
86300d93d79Smatthias.ringwald 
86400d93d79Smatthias.ringwald     // general commands without an assigned channel
86500d93d79Smatthias.ringwald     switch(code) {
86600d93d79Smatthias.ringwald 
86700d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
86800d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
86900d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
87000d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
8712b83fb7dSmatthias.ringwald             return;
87200d93d79Smatthias.ringwald         }
87300d93d79Smatthias.ringwald 
8742b360848Smatthias.ringwald         case ECHO_REQUEST:
8752b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
8762b83fb7dSmatthias.ringwald             return;
87700d93d79Smatthias.ringwald 
87800d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
87900d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
8802b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
8812b83fb7dSmatthias.ringwald             return;
88200d93d79Smatthias.ringwald         }
88300d93d79Smatthias.ringwald 
88400d93d79Smatthias.ringwald         default:
88500d93d79Smatthias.ringwald             break;
88600d93d79Smatthias.ringwald     }
88700d93d79Smatthias.ringwald 
88800d93d79Smatthias.ringwald 
88900d93d79Smatthias.ringwald     // Get potential destination CID
89000d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
89100d93d79Smatthias.ringwald 
89200d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
89300d93d79Smatthias.ringwald     linked_item_t *it;
89400d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
89500d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
89600d93d79Smatthias.ringwald         if (channel->handle == handle) {
89700d93d79Smatthias.ringwald             if (code & 1) {
898b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
899b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
90000d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9014e32727eSmatthias.ringwald                     break;
90200d93d79Smatthias.ringwald                 }
90300d93d79Smatthias.ringwald             } else {
904b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
90500d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
90600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9074e32727eSmatthias.ringwald                     break;
90800d93d79Smatthias.ringwald                 }
90900d93d79Smatthias.ringwald             }
91000d93d79Smatthias.ringwald         }
91100d93d79Smatthias.ringwald     }
91200d93d79Smatthias.ringwald }
91300d93d79Smatthias.ringwald 
91400d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
91500d93d79Smatthias.ringwald 
91600d93d79Smatthias.ringwald     // Get Channel ID
91700d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
91800d93d79Smatthias.ringwald 
91900d93d79Smatthias.ringwald     // Signaling Packet?
92000d93d79Smatthias.ringwald     if (channel_id == 1) {
92100d93d79Smatthias.ringwald 
92200d93d79Smatthias.ringwald         // Get Connection
92300d93d79Smatthias.ringwald         hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
92400d93d79Smatthias.ringwald 
92500d93d79Smatthias.ringwald         uint16_t command_offset = 8;
92600d93d79Smatthias.ringwald         while (command_offset < size) {
92700d93d79Smatthias.ringwald 
92800d93d79Smatthias.ringwald             // handle signaling commands
92900d93d79Smatthias.ringwald             l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
93000d93d79Smatthias.ringwald 
93100d93d79Smatthias.ringwald             // increment command_offset
93200d93d79Smatthias.ringwald             command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
93300d93d79Smatthias.ringwald         }
93464472d52Smatthias.ringwald 
93564472d52Smatthias.ringwald         l2cap_run();
93664472d52Smatthias.ringwald 
93700d93d79Smatthias.ringwald         return;
93800d93d79Smatthias.ringwald     }
93900d93d79Smatthias.ringwald 
94000d93d79Smatthias.ringwald     // Find channel for this channel_id and connection handle
94100d93d79Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
94200d93d79Smatthias.ringwald     if (channel) {
94358de5610Smatthias.ringwald         l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
94400d93d79Smatthias.ringwald     }
94500d93d79Smatthias.ringwald }
94600d93d79Smatthias.ringwald 
9472718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
9482718e2e7Smatthias.ringwald     switch (packet_type) {
9492718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
9502718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
9512718e2e7Smatthias.ringwald             break;
9522718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
9532718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
9542718e2e7Smatthias.ringwald             break;
9552718e2e7Smatthias.ringwald         default:
9562718e2e7Smatthias.ringwald             break;
9572718e2e7Smatthias.ringwald     }
9582718e2e7Smatthias.ringwald }
95900d93d79Smatthias.ringwald 
96015ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
96127a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
962f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
963f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
964f62db1e3Smatthias.ringwald     // discard channel
965f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
966d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
967c8e4258aSmatthias.ringwald }
9681e6aba47Smatthias.ringwald 
9699d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
970c52bf64dSmatthias.ringwald     linked_item_t *it;
9719d9bbc01Smatthias.ringwald 
9729d9bbc01Smatthias.ringwald     // close open channels
9739d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
9749d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
9759d9bbc01Smatthias.ringwald         if ( service->psm == psm){
9769d9bbc01Smatthias.ringwald             return service;
9779d9bbc01Smatthias.ringwald         };
9789d9bbc01Smatthias.ringwald     }
9799d9bbc01Smatthias.ringwald     return NULL;
9809d9bbc01Smatthias.ringwald }
9819d9bbc01Smatthias.ringwald 
98236944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
9834bb582b6Smatthias.ringwald     // check for alread registered psm
9844bb582b6Smatthias.ringwald     // TODO: emit error event
9859d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
986277abc2cSmatthias.ringwald     if (service) {
987277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
98881476041Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
989277abc2cSmatthias.ringwald         return;
990277abc2cSmatthias.ringwald     }
9919d9bbc01Smatthias.ringwald 
9924bb582b6Smatthias.ringwald     // alloc structure
9934bb582b6Smatthias.ringwald     // TODO: emit error event
994*28ca2b46S[email protected]     service = (l2cap_service_t *) btstack_memory_l2cap_service_get();
995277abc2cSmatthias.ringwald     if (!service) {
996277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
9975842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
998277abc2cSmatthias.ringwald         return;
999277abc2cSmatthias.ringwald     }
10009d9bbc01Smatthias.ringwald 
10019d9bbc01Smatthias.ringwald     // fill in
10029d9bbc01Smatthias.ringwald     service->psm = psm;
10039d9bbc01Smatthias.ringwald     service->mtu = mtu;
10049d9bbc01Smatthias.ringwald     service->connection = connection;
1005d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
10069d9bbc01Smatthias.ringwald 
10079d9bbc01Smatthias.ringwald     // add to services list
10089d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
1009c0e866bfSmatthias.ringwald 
1010c0e866bfSmatthias.ringwald     // enable page scan
1011c0e866bfSmatthias.ringwald     hci_connectable_control(1);
10125842b6d9Smatthias.ringwald 
10135842b6d9Smatthias.ringwald     // done
10145842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
10159d9bbc01Smatthias.ringwald }
10169d9bbc01Smatthias.ringwald 
101736944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
10189d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1019037d6e48Smatthias.ringwald     if (!service) return;
10209d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1021d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1022c0e866bfSmatthias.ringwald 
1023c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1024c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1025c0e866bfSmatthias.ringwald     hci_connectable_control(0);
10269d9bbc01Smatthias.ringwald }
10279d9bbc01Smatthias.ringwald 
10289d9bbc01Smatthias.ringwald //
102936944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
10309d9bbc01Smatthias.ringwald     linked_item_t *it;
10319d9bbc01Smatthias.ringwald 
10323a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1033c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1034c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1035c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1036c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1037cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1038c52bf64dSmatthias.ringwald         }
1039c52bf64dSmatthias.ringwald     }
10409d9bbc01Smatthias.ringwald 
1041645658c9Smatthias.ringwald     // unregister services
104269025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
104369025de8Smatthias.ringwald     while (it->next) {
104469025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1045645658c9Smatthias.ringwald         if (service->connection == connection){
104669025de8Smatthias.ringwald             it->next = it->next->next;
1047d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
10488149d2c7Smatthias.ringwald         } else {
10498149d2c7Smatthias.ringwald             it = it->next;
1050645658c9Smatthias.ringwald         }
1051645658c9Smatthias.ringwald     }
1052cb8eb7e6Smatthias.ringwald 
1053cb8eb7e6Smatthias.ringwald     // process
1054cb8eb7e6Smatthias.ringwald     l2cap_run();
1055c52bf64dSmatthias.ringwald }
1056