xref: /btstack/src/l2cap.c (revision 4cf56b4aa05285007240e348cd54565b35032519)
143625864Smatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 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.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
3843625864Smatthias.ringwald  *  l2cap.c
3943625864Smatthias.ringwald  *
4043625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
4143625864Smatthias.ringwald  *
4243625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
4343625864Smatthias.ringwald  */
4443625864Smatthias.ringwald 
4543625864Smatthias.ringwald #include "l2cap.h"
46645658c9Smatthias.ringwald #include "hci.h"
472b3c6c9bSmatthias.ringwald #include "hci_dump.h"
486218e6f1Smatthias.ringwald #include "debug.h"
49d3a9df87Smatthias.ringwald #include "btstack_memory.h"
5043625864Smatthias.ringwald 
5143625864Smatthias.ringwald #include <stdarg.h>
5243625864Smatthias.ringwald #include <string.h>
5343625864Smatthias.ringwald 
5443625864Smatthias.ringwald #include <stdio.h>
5543625864Smatthias.ringwald 
564c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
574c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
584c744e21Smatthias.ringwald 
5939bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
60e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6139bda6d5Smatthias.ringwald 
6200d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6700d93d79Smatthias.ringwald 
6839bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
6939bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
7039bda6d5Smatthias.ringwald 
7139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
722b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
732b83fb7dSmatthias.ringwald static int signaling_responses_pending;
742b83fb7dSmatthias.ringwald 
751e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL;
769d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL;
7736944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
78808a48abSmatthias.ringwald static int new_credits_blocked = 0;
791e6aba47Smatthias.ringwald 
805652b5ffS[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler = NULL;
815652b5ffS[email protected] static btstack_packet_handler_t security_protocol_packet_handler = NULL;
825652b5ffS[email protected] 
8339bda6d5Smatthias.ringwald // prototypes
84fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
85fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm);
86fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
87fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
88fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
89fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
9039bda6d5Smatthias.ringwald 
9139bda6d5Smatthias.ringwald 
921e6aba47Smatthias.ringwald void l2cap_init(){
93808a48abSmatthias.ringwald     new_credits_blocked = 0;
942b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
95808a48abSmatthias.ringwald 
96f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
97f5454fc6Smatthias.ringwald     l2cap_services = NULL;
98f5454fc6Smatthias.ringwald 
99f5454fc6Smatthias.ringwald     packet_handler = null_packet_handler;
100f5454fc6Smatthias.ringwald 
101fcadd0caSmatthias.ringwald     //
1022718e2e7Smatthias.ringwald     // register callback with HCI
103fcadd0caSmatthias.ringwald     //
1042718e2e7Smatthias.ringwald     hci_register_packet_handler(&l2cap_packet_handler);
105c0e866bfSmatthias.ringwald     hci_connectable_control(0); // no services yet
106fcadd0caSmatthias.ringwald }
107fcadd0caSmatthias.ringwald 
108fcadd0caSmatthias.ringwald 
109fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */
11036944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
111fcadd0caSmatthias.ringwald }
11236944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
113b502e1b0Smatthias.ringwald     packet_handler = handler;
1141e6aba47Smatthias.ringwald }
1151e6aba47Smatthias.ringwald 
11658de5610Smatthias.ringwald //  notify client/protocol handler
11758de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
11858de5610Smatthias.ringwald     if (channel->packet_handler) {
11958de5610Smatthias.ringwald         (* (channel->packet_handler))(type, channel->local_cid, data, size);
12058de5610Smatthias.ringwald     } else {
12136944dffSmatthias.ringwald         (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
12258de5610Smatthias.ringwald     }
12358de5610Smatthias.ringwald }
12458de5610Smatthias.ringwald 
12558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
1269893b714Smatthias.ringwald     uint8_t event[21];
12758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
12858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
12958de5610Smatthias.ringwald     event[2] = status;
13058de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
13158de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
13258de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
13358de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
13458de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1354c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1364c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
13758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
13858de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
13958de5610Smatthias.ringwald }
14058de5610Smatthias.ringwald 
14158de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
14258de5610Smatthias.ringwald     uint8_t event[4];
14358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
14458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
14558de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
14658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
14758de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
14858de5610Smatthias.ringwald }
14958de5610Smatthias.ringwald 
15058de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
15158de5610Smatthias.ringwald     uint8_t event[16];
15258de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
15358de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
15458de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
15558de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
15658de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
15758de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
15858de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
15958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
16058de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1610af41d30Smatthias.ringwald }
162808a48abSmatthias.ringwald 
1635842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
1645842b6d9Smatthias.ringwald     uint8_t event[5];
1655842b6d9Smatthias.ringwald     event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
1665842b6d9Smatthias.ringwald     event[1] = sizeof(event) - 2;
1675842b6d9Smatthias.ringwald     event[2] = status;
1685842b6d9Smatthias.ringwald     bt_store_16(event, 3, psm);
1695842b6d9Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1705842b6d9Smatthias.ringwald     (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
1715842b6d9Smatthias.ringwald }
1725842b6d9Smatthias.ringwald 
1736218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
1746218e6f1Smatthias.ringwald     // track credits
1756218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1767b5fbe1fSmatthias.ringwald     // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
1776218e6f1Smatthias.ringwald 
1786218e6f1Smatthias.ringwald     uint8_t event[5];
1796218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1806218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1816218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1826218e6f1Smatthias.ringwald     event[4] = credits;
1836218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1846218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1856218e6f1Smatthias.ringwald }
1866218e6f1Smatthias.ringwald 
187808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
188808a48abSmatthias.ringwald     new_credits_blocked = blocked;
189808a48abSmatthias.ringwald }
190808a48abSmatthias.ringwald 
19140d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
192808a48abSmatthias.ringwald 
193808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
194808a48abSmatthias.ringwald 
1958d371091Smatthias.ringwald     linked_item_t *it;
1968d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1978d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
1988d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
1990e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2004c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2018d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2028d371091Smatthias.ringwald         }
2038d371091Smatthias.ringwald     }
2048d371091Smatthias.ringwald }
2058d371091Smatthias.ringwald 
206b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
207f62db1e3Smatthias.ringwald     linked_item_t *it;
208f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2098d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
210b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
211f62db1e3Smatthias.ringwald             return channel;
212f62db1e3Smatthias.ringwald         }
213f62db1e3Smatthias.ringwald     }
214f62db1e3Smatthias.ringwald     return NULL;
215f62db1e3Smatthias.ringwald }
216f62db1e3Smatthias.ringwald 
2176b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2186b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2196b1fde37Smatthias.ringwald     if (!channel) return 0;
2206b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2216b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2226b1fde37Smatthias.ringwald }
2236b1fde37Smatthias.ringwald 
22496cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
22596cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
22696cbd662Smatthias.ringwald     if (channel) {
22796cbd662Smatthias.ringwald         return channel->remote_mtu;
22896cbd662Smatthias.ringwald     }
22996cbd662Smatthias.ringwald     return 0;
23096cbd662Smatthias.ringwald }
23196cbd662Smatthias.ringwald 
23258de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
233b1d43497Smatthias.ringwald 
234b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
235b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
236b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
237b1d43497Smatthias.ringwald     }
238b1d43497Smatthias.ringwald 
2397b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
240b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
24158de5610Smatthias.ringwald     va_list argptr;
24258de5610Smatthias.ringwald     va_start(argptr, identifier);
243816c0598Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
24458de5610Smatthias.ringwald     va_end(argptr);
2457b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
246816c0598Smatthias.ringwald     return hci_send_acl_packet(acl_buffer, len);
24758de5610Smatthias.ringwald }
24858de5610Smatthias.ringwald 
249b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
250b1d43497Smatthias.ringwald     return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
251b1d43497Smatthias.ringwald }
2526218e6f1Smatthias.ringwald 
253b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
254b1d43497Smatthias.ringwald 
255b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
256b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
257808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2588ea03fa5Smatthias.ringwald     }
2596218e6f1Smatthias.ringwald 
26058de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
261b1d43497Smatthias.ringwald     if (!channel) {
262b1d43497Smatthias.ringwald         log_error("l2cap_send_internal no channel for cid %u\n", local_cid);
263b1d43497Smatthias.ringwald         return -1;   // TODO: define error
2646218e6f1Smatthias.ringwald     }
2656218e6f1Smatthias.ringwald 
266b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
267b1d43497Smatthias.ringwald         log_error("l2cap_send_internal cid %u, no credits!\n", local_cid);
268b1d43497Smatthias.ringwald         return -1;  // TODO: define error
269b1d43497Smatthias.ringwald     }
270b1d43497Smatthias.ringwald 
271b1d43497Smatthias.ringwald     --channel->packets_granted;
272b1d43497Smatthias.ringwald 
273b1d43497Smatthias.ringwald     log_debug("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
274b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
275b1d43497Smatthias.ringwald 
276b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
277b1d43497Smatthias.ringwald 
27858de5610Smatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
27958de5610Smatthias.ringwald     bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
28058de5610Smatthias.ringwald     // 2 - ACL length
28158de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
28258de5610Smatthias.ringwald     // 4 - L2CAP packet length
28358de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
28458de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
28558de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
28658de5610Smatthias.ringwald     // send
287b1d43497Smatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
28891b99603Smatthias.ringwald 
28991b99603Smatthias.ringwald     l2cap_hand_out_credits();
29091b99603Smatthias.ringwald 
2916218e6f1Smatthias.ringwald     return err;
29258de5610Smatthias.ringwald }
29358de5610Smatthias.ringwald 
2942149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
2952149f12eSmatthias.ringwald 
2962149f12eSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
2972149f12eSmatthias.ringwald         log_info("l2cap_send_prepared_to_handle cid %u, cannot send\n", cid);
2982149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
2992149f12eSmatthias.ringwald     }
3002149f12eSmatthias.ringwald 
3012149f12eSmatthias.ringwald     log_debug("l2cap_send_prepared_to_handle cid %u, handle %u\n", cid, handle);
3022149f12eSmatthias.ringwald 
3032149f12eSmatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
3042149f12eSmatthias.ringwald 
3052149f12eSmatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
3062149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14));
3072149f12eSmatthias.ringwald     // 2 - ACL length
3082149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
3092149f12eSmatthias.ringwald     // 4 - L2CAP packet length
3102149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
3112149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
3122149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 6, cid);
3132149f12eSmatthias.ringwald     // send
3142149f12eSmatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
3152149f12eSmatthias.ringwald 
3162149f12eSmatthias.ringwald     l2cap_hand_out_credits();
3172149f12eSmatthias.ringwald 
3182149f12eSmatthias.ringwald     return err;
3192149f12eSmatthias.ringwald }
3202149f12eSmatthias.ringwald 
321b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
322b1d43497Smatthias.ringwald 
323b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
324b1d43497Smatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
325b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
326b1d43497Smatthias.ringwald     }
327b1d43497Smatthias.ringwald 
328b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
329b1d43497Smatthias.ringwald 
330b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
331b1d43497Smatthias.ringwald 
332b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
333b1d43497Smatthias.ringwald }
334b1d43497Smatthias.ringwald 
3352149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
3362149f12eSmatthias.ringwald 
3372149f12eSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
3382149f12eSmatthias.ringwald         log_info("l2cap_send_internal cid %u, cannot send\n", cid);
3392149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3402149f12eSmatthias.ringwald     }
3412149f12eSmatthias.ringwald 
3422149f12eSmatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
3432149f12eSmatthias.ringwald 
3442149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
3452149f12eSmatthias.ringwald 
3462149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
3472149f12eSmatthias.ringwald }
3482149f12eSmatthias.ringwald 
34928ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
35028ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
35128ca2b46S[email protected] }
35228ca2b46S[email protected] 
35328ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
35428ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
35528ca2b46S[email protected] }
35628ca2b46S[email protected] 
35728ca2b46S[email protected] 
358b1d43497Smatthias.ringwald 
3598158c421Smatthias.ringwald // MARK: L2CAP_RUN
3602cd0be45Smatthias.ringwald // process outstanding signaling tasks
3612cd0be45Smatthias.ringwald void l2cap_run(void){
3622b83fb7dSmatthias.ringwald 
3632b83fb7dSmatthias.ringwald     // check pending signaling responses
3642b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
3652b83fb7dSmatthias.ringwald 
36602b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
3672b83fb7dSmatthias.ringwald 
3682b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
3692b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
3702b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
3712b360848Smatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST
3722b83fb7dSmatthias.ringwald 
3732b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
3742b360848Smatthias.ringwald             case CONNECTION_REQUEST:
3752b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
3762b360848Smatthias.ringwald                 break;
3772b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
3782b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
3792b83fb7dSmatthias.ringwald                 break;
3802b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
3812b83fb7dSmatthias.ringwald                 if (infoType == 2) {
3822b83fb7dSmatthias.ringwald                     uint32_t features = 0;
3832b83fb7dSmatthias.ringwald                     // extended features request supported, however no features present
3842b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
3852b83fb7dSmatthias.ringwald                 } else {
3862b83fb7dSmatthias.ringwald                     // all other types are not supported
3872b83fb7dSmatthias.ringwald                     l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
3882b83fb7dSmatthias.ringwald                 }
3892b83fb7dSmatthias.ringwald                 break;
3902b83fb7dSmatthias.ringwald             default:
3912b83fb7dSmatthias.ringwald                 // should not happen
3922b83fb7dSmatthias.ringwald                 break;
3932b83fb7dSmatthias.ringwald         }
3942b83fb7dSmatthias.ringwald 
3952b83fb7dSmatthias.ringwald         // remove first item
3962b83fb7dSmatthias.ringwald         signaling_responses_pending--;
3972b83fb7dSmatthias.ringwald         int i;
3982b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
39976115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
4002b83fb7dSmatthias.ringwald         }
4012b83fb7dSmatthias.ringwald     }
4022b83fb7dSmatthias.ringwald 
403ae280e73Smatthias.ringwald     uint8_t  config_options[4];
4042cd0be45Smatthias.ringwald     linked_item_t *it;
405756102d3Smatthias.ringwald     linked_item_t *next;
406756102d3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = next){
407756102d3Smatthias.ringwald         next = it->next;    // cache next item as current item might get freed
4082cd0be45Smatthias.ringwald 
40902b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
41002b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
4112cd0be45Smatthias.ringwald 
4122cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
4136e6710ebSmatthias.ringwald 
4147b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
4156e6710ebSmatthias.ringwald 
41656081214Smatthias.ringwald 
4172cd0be45Smatthias.ringwald         switch (channel->state){
4182cd0be45Smatthias.ringwald 
41902b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
42064472d52Smatthias.ringwald                 // send connection request - set state first
42164472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
42202b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
4238f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
42402b22dc4Smatthias.ringwald                 break;
42502b22dc4Smatthias.ringwald 
426e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
427b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
428e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
429756102d3Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list
430d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
431e7ff783cSmatthias.ringwald                 break;
432e7ff783cSmatthias.ringwald 
433552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
434fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
43528ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
4362a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
437552d92a1Smatthias.ringwald                 break;
438552d92a1Smatthias.ringwald 
4396fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
4406fdcc387Smatthias.ringwald                 // success, start l2cap handshake
441b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4426fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
4432a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
4446fdcc387Smatthias.ringwald                 break;
4456fdcc387Smatthias.ringwald 
446fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
44773cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
44828ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
44928ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
450fa8473a4Smatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
451fa8473a4Smatthias.ringwald                 }
45273cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
45328ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
45428ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
455b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
456ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
457ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
458ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
459b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
460fa8473a4Smatthias.ringwald                 }
461fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
462552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
463552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
464552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
465fa8473a4Smatthias.ringwald                 }
466552d92a1Smatthias.ringwald                 break;
467552d92a1Smatthias.ringwald 
468e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
469b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
470756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
471e7ff783cSmatthias.ringwald                 break;
472e7ff783cSmatthias.ringwald 
473e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
474b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
4752cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
4762a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
4772cd0be45Smatthias.ringwald                 break;
4782cd0be45Smatthias.ringwald             default:
4792cd0be45Smatthias.ringwald                 break;
4802cd0be45Smatthias.ringwald         }
4812cd0be45Smatthias.ringwald     }
4822cd0be45Smatthias.ringwald }
4832cd0be45Smatthias.ringwald 
4844aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
485816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
486fa8c92f6Smatthias.ringwald }
487fa8c92f6Smatthias.ringwald 
4881e6aba47Smatthias.ringwald // open outgoing L2CAP channel
48915470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
49015470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
4911e6aba47Smatthias.ringwald 
4921e6aba47Smatthias.ringwald     // alloc structure
49328ca2b46S[email protected]     l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
4942b360848Smatthias.ringwald     if (!chan) {
4952b360848Smatthias.ringwald         // emit error event
4962b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
4972b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
4982b360848Smatthias.ringwald         dummy_channel.psm = psm;
4992b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
5002b360848Smatthias.ringwald         return;
5012b360848Smatthias.ringwald     }
5021d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
5032985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
5042985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
5059775e25bSmatthias.ringwald     }
5069775e25bSmatthias.ringwald 
5071e6aba47Smatthias.ringwald     // fill in
5081e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
5091e6aba47Smatthias.ringwald     chan->psm = psm;
5101e6aba47Smatthias.ringwald     chan->handle = 0;
5111e6aba47Smatthias.ringwald     chan->connection = connection;
5126b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
5132784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
51415470d27Smatthias.ringwald     chan->local_mtu = mtu;
5156218e6f1Smatthias.ringwald     chan->packets_granted = 0;
5166218e6f1Smatthias.ringwald 
5171e6aba47Smatthias.ringwald     // set initial state
51802b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
51973cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
520b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
521b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
5221e6aba47Smatthias.ringwald 
5231e6aba47Smatthias.ringwald     // add to connections list
5241e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
5251e6aba47Smatthias.ringwald 
52602b22dc4Smatthias.ringwald     l2cap_run();
52743625864Smatthias.ringwald }
52843625864Smatthias.ringwald 
529b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
530b35f641cSmatthias.ringwald     // find channel for local_cid
531b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
532f62db1e3Smatthias.ringwald     if (channel) {
533e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
534f62db1e3Smatthias.ringwald     }
5352cd0be45Smatthias.ringwald     // process
5362cd0be45Smatthias.ringwald     l2cap_run();
53743625864Smatthias.ringwald }
5381e6aba47Smatthias.ringwald 
539afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
54015ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
54115ec09bbSmatthias.ringwald     while (it->next){
54215ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
543b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
54402b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
545afde0c52Smatthias.ringwald                 // failure, forward error code
546afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
547afde0c52Smatthias.ringwald                 // discard channel
54815ec09bbSmatthias.ringwald                 it->next = it->next->next;
549d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
550afde0c52Smatthias.ringwald             }
55115ec09bbSmatthias.ringwald         } else {
55215ec09bbSmatthias.ringwald             it = it->next;
553afde0c52Smatthias.ringwald         }
554afde0c52Smatthias.ringwald     }
555afde0c52Smatthias.ringwald }
556afde0c52Smatthias.ringwald 
557afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
558afde0c52Smatthias.ringwald     linked_item_t *it;
559afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
560afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
561afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
56202b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
563b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
5646fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
565afde0c52Smatthias.ringwald                 channel->handle = handle;
566b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
567afde0c52Smatthias.ringwald             }
568afde0c52Smatthias.ringwald         }
569afde0c52Smatthias.ringwald     }
5706fdcc387Smatthias.ringwald     // process
5716fdcc387Smatthias.ringwald     l2cap_run();
572afde0c52Smatthias.ringwald }
573b448a0e7Smatthias.ringwald 
574afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
575afde0c52Smatthias.ringwald 
576afde0c52Smatthias.ringwald     bd_addr_t address;
577afde0c52Smatthias.ringwald     hci_con_handle_t handle;
5786e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
57936944dffSmatthias.ringwald     linked_item_t *it;
5802d00edd4Smatthias.ringwald     int hci_con_used;
581afde0c52Smatthias.ringwald 
582afde0c52Smatthias.ringwald     switch(packet[0]){
583afde0c52Smatthias.ringwald 
584afde0c52Smatthias.ringwald         // handle connection complete events
585afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
586afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
587afde0c52Smatthias.ringwald             if (packet[2] == 0){
588afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
589afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
590afde0c52Smatthias.ringwald             } else {
591afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
592afde0c52Smatthias.ringwald             }
593afde0c52Smatthias.ringwald             break;
594afde0c52Smatthias.ringwald 
595afde0c52Smatthias.ringwald         // handle successful create connection cancel command
596afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
597afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
598afde0c52Smatthias.ringwald                 if (packet[5] == 0){
599afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
600afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
601afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
60203cfbabcSmatthias.ringwald                 }
6031e6aba47Smatthias.ringwald             }
60439d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
60539d59809Smatthias.ringwald             break;
60639d59809Smatthias.ringwald 
60739d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
60839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
609afde0c52Smatthias.ringwald             break;
61027a923d0Smatthias.ringwald 
6111e6aba47Smatthias.ringwald         // handle disconnection complete events
612afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
61327a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
614afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
61515ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
61615ec09bbSmatthias.ringwald             while (it->next){
61739ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
61827a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
61915ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
6202060cf14Smatthias.ringwald                     it->next = it->next->next;
62115ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
622d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
62315ec09bbSmatthias.ringwald                 } else {
62415ec09bbSmatthias.ringwald                     it = it->next;
62527a923d0Smatthias.ringwald                 }
62627a923d0Smatthias.ringwald             }
627afde0c52Smatthias.ringwald             break;
628fcadd0caSmatthias.ringwald 
6296218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
63002b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
6318d371091Smatthias.ringwald             l2cap_hand_out_credits();
6326218e6f1Smatthias.ringwald             break;
6336218e6f1Smatthias.ringwald 
634ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
635afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
63636944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
63780ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
6382d00edd4Smatthias.ringwald             hci_con_used = 0;
639ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
640ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
641ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
6422d00edd4Smatthias.ringwald                     hci_con_used = 1;
643ee091cf1Smatthias.ringwald                 }
644ee091cf1Smatthias.ringwald             }
6452d00edd4Smatthias.ringwald             if (hci_con_used) break;
64632ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
6479edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
648afde0c52Smatthias.ringwald             break;
649ee091cf1Smatthias.ringwald 
6506e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
6516e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
6526e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
6536e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
6546e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
6556e6710ebSmatthias.ringwald                 }
6566e6710ebSmatthias.ringwald             }
6575652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
6585652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
6595652b5ffS[email protected]             }
6605652b5ffS[email protected]             if (security_protocol_packet_handler) {
661133efcfdSmatthias.ringwald                 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
6625652b5ffS[email protected]             }
6636e6710ebSmatthias.ringwald             break;
6646e6710ebSmatthias.ringwald 
665afde0c52Smatthias.ringwald         default:
666afde0c52Smatthias.ringwald             break;
667afde0c52Smatthias.ringwald     }
668afde0c52Smatthias.ringwald 
669afde0c52Smatthias.ringwald     // pass on
670b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
6711e6aba47Smatthias.ringwald }
6721e6aba47Smatthias.ringwald 
673afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
674b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
675e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
676e7ff783cSmatthias.ringwald     l2cap_run();
67784836b65Smatthias.ringwald }
67884836b65Smatthias.ringwald 
6792b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
680*4cf56b4aSmatthias.ringwald     // Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused."
6812b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
6822b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
6832b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
6842b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
6852b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
6862b360848Smatthias.ringwald         signaling_responses_pending++;
6872b360848Smatthias.ringwald         l2cap_run();
6882b360848Smatthias.ringwald     }
6892b360848Smatthias.ringwald }
6902b360848Smatthias.ringwald 
691b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
692645658c9Smatthias.ringwald 
6937b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
694645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
695645658c9Smatthias.ringwald     if (!service) {
696645658c9Smatthias.ringwald         // 0x0002 PSM not supported
6972b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
698645658c9Smatthias.ringwald         return;
699645658c9Smatthias.ringwald     }
700645658c9Smatthias.ringwald 
701645658c9Smatthias.ringwald     hci_connection_t * hci_connection = connection_for_handle( handle );
702645658c9Smatthias.ringwald     if (!hci_connection) {
7032b360848Smatthias.ringwald         //
7047d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
705645658c9Smatthias.ringwald         return;
706645658c9Smatthias.ringwald     }
707645658c9Smatthias.ringwald     // alloc structure
7087b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
70928ca2b46S[email protected]     l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
7102b360848Smatthias.ringwald     if (!channel){
7112b360848Smatthias.ringwald         // 0x0004 No resources available
7122b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
7132b360848Smatthias.ringwald         return;
7142b360848Smatthias.ringwald     }
715645658c9Smatthias.ringwald 
716645658c9Smatthias.ringwald     // fill in
717169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
718169f8b28Smatthias.ringwald     channel->psm = psm;
719169f8b28Smatthias.ringwald     channel->handle = handle;
720169f8b28Smatthias.ringwald     channel->connection = service->connection;
721f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
722b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
723b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
724fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
7250a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
726761b0451Smatthias.ringwald     channel->packets_granted = 0;
727b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
728645658c9Smatthias.ringwald 
7291d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
7302985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
7312985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
7329775e25bSmatthias.ringwald     }
7339775e25bSmatthias.ringwald 
734645658c9Smatthias.ringwald     // set initial state
735e405ae81Smatthias.ringwald     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
73673cf2b3dSmatthias.ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
737e405ae81Smatthias.ringwald 
738645658c9Smatthias.ringwald     // add to connections list
739169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
740645658c9Smatthias.ringwald 
741e405ae81Smatthias.ringwald     // emit incoming connection request
742e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
743e405ae81Smatthias.ringwald }
744645658c9Smatthias.ringwald 
745b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
746b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
747e405ae81Smatthias.ringwald     if (!channel) {
7487d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
749e405ae81Smatthias.ringwald         return;
750e405ae81Smatthias.ringwald     }
751e405ae81Smatthias.ringwald 
752552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
753e405ae81Smatthias.ringwald 
754552d92a1Smatthias.ringwald     // process
755552d92a1Smatthias.ringwald     l2cap_run();
756e405ae81Smatthias.ringwald }
757645658c9Smatthias.ringwald 
758b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
759b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
760e405ae81Smatthias.ringwald     if (!channel) {
7617d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
762e405ae81Smatthias.ringwald         return;
763e405ae81Smatthias.ringwald     }
764e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
765e7ff783cSmatthias.ringwald     channel->reason = reason;
766e7ff783cSmatthias.ringwald     l2cap_run();
767645658c9Smatthias.ringwald }
768645658c9Smatthias.ringwald 
7692784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
770b1988dceSmatthias.ringwald 
771b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
772b1988dceSmatthias.ringwald 
7732784b77dSmatthias.ringwald     // accept the other's configuration options
7743de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
7753de7c0caSmatthias.ringwald     uint16_t pos     = 8;
7763de7c0caSmatthias.ringwald     while (pos < end_pos){
7771dc511deSmatthias.ringwald         uint8_t type   = command[pos++];
7781dc511deSmatthias.ringwald         uint8_t length = command[pos++];
7791dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
7801dc511deSmatthias.ringwald         if ((type & 0x7f) == 1 && length == 2){
7811dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
7827b5fbe1fSmatthias.ringwald             // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
7831dc511deSmatthias.ringwald         }
7841dc511deSmatthias.ringwald         pos += length;
7851dc511deSmatthias.ringwald     }
7862784b77dSmatthias.ringwald }
7872784b77dSmatthias.ringwald 
788fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
7897b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
79073cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
79173cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
792fa8473a4Smatthias.ringwald     return 1;
793fa8473a4Smatthias.ringwald }
794fa8473a4Smatthias.ringwald 
795fa8473a4Smatthias.ringwald 
79600d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
7971e6aba47Smatthias.ringwald 
79800d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
79900d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
80038e5900eSmatthias.ringwald     uint16_t result = 0;
8011e6aba47Smatthias.ringwald 
8025842b6d9Smatthias.ringwald     log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
803b35f641cSmatthias.ringwald 
8049a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
8059a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
8069a011532Smatthias.ringwald         switch (channel->state){
807fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
8089a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
8092b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
8109a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
8119a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
8129a011532Smatthias.ringwald                 break;
8139a011532Smatthias.ringwald 
8149a011532Smatthias.ringwald             default:
8159a011532Smatthias.ringwald                 // ignore in other states
8169a011532Smatthias.ringwald                 break;
8179a011532Smatthias.ringwald         }
8189a011532Smatthias.ringwald         return;
8199a011532Smatthias.ringwald     }
8209a011532Smatthias.ringwald 
82156081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
8221e6aba47Smatthias.ringwald     switch (channel->state) {
8231e6aba47Smatthias.ringwald 
8241e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
8251e6aba47Smatthias.ringwald             switch (code){
8261e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
82700d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
82838e5900eSmatthias.ringwald                     switch (result) {
82938e5900eSmatthias.ringwald                         case 0:
830169f8b28Smatthias.ringwald                             // successful connection
83100d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
832fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
83328ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
83438e5900eSmatthias.ringwald                             break;
83538e5900eSmatthias.ringwald                         case 1:
83638e5900eSmatthias.ringwald                             // connection pending. get some coffee
83738e5900eSmatthias.ringwald                             break;
83838e5900eSmatthias.ringwald                         default:
839eb920dbeSmatthias.ringwald                             // channel closed
840eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
841eb920dbeSmatthias.ringwald 
842f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
84338e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
844eb920dbeSmatthias.ringwald 
845eb920dbeSmatthias.ringwald                             // drop link key if security block
846eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
847eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
848eb920dbeSmatthias.ringwald                             }
849eb920dbeSmatthias.ringwald 
850eb920dbeSmatthias.ringwald                             // discard channel
851eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
852d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
85338e5900eSmatthias.ringwald                             break;
8541e6aba47Smatthias.ringwald                     }
8551e6aba47Smatthias.ringwald                     break;
85638e5900eSmatthias.ringwald 
85738e5900eSmatthias.ringwald                 default:
8581e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
85938e5900eSmatthias.ringwald                     break;
8601e6aba47Smatthias.ringwald             }
8611e6aba47Smatthias.ringwald             break;
8621e6aba47Smatthias.ringwald 
863fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
864ae280e73Smatthias.ringwald             switch (code) {
865ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
86628ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
86728ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
868ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
869ae280e73Smatthias.ringwald                     break;
8701e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
87128ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
8725a67bd4aSmatthias.ringwald                     break;
8735a67bd4aSmatthias.ringwald                 default:
8745a67bd4aSmatthias.ringwald                     break;
8751e6aba47Smatthias.ringwald             }
876fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
877fa8473a4Smatthias.ringwald                 // for open:
8785a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
879fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
8806218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
881c8e4258aSmatthias.ringwald             }
882c8e4258aSmatthias.ringwald             break;
883f62db1e3Smatthias.ringwald 
884f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
885f62db1e3Smatthias.ringwald             switch (code) {
886f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
88727a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
88827a923d0Smatthias.ringwald                     break;
8895a67bd4aSmatthias.ringwald                 default:
8905a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
8915a67bd4aSmatthias.ringwald                     break;
89227a923d0Smatthias.ringwald             }
89327a923d0Smatthias.ringwald             break;
89484836b65Smatthias.ringwald 
89584836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
89684836b65Smatthias.ringwald             // @TODO handle incoming requests
89784836b65Smatthias.ringwald             break;
89884836b65Smatthias.ringwald 
89984836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
90084836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
90184836b65Smatthias.ringwald             break;
90210642e45Smatthias.ringwald         default:
90310642e45Smatthias.ringwald             break;
90427a923d0Smatthias.ringwald     }
9057b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
90627a923d0Smatthias.ringwald }
90727a923d0Smatthias.ringwald 
90800d93d79Smatthias.ringwald 
90900d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
91000d93d79Smatthias.ringwald 
91100d93d79Smatthias.ringwald     // get code, signalind identifier and command len
91200d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
91300d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
91400d93d79Smatthias.ringwald 
91500d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
91600d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
91700d93d79Smatthias.ringwald         return;
91800d93d79Smatthias.ringwald     }
91900d93d79Smatthias.ringwald 
92000d93d79Smatthias.ringwald     // general commands without an assigned channel
92100d93d79Smatthias.ringwald     switch(code) {
92200d93d79Smatthias.ringwald 
92300d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
92400d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
92500d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
92600d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
9272b83fb7dSmatthias.ringwald             return;
92800d93d79Smatthias.ringwald         }
92900d93d79Smatthias.ringwald 
9302b360848Smatthias.ringwald         case ECHO_REQUEST:
9312b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
9322b83fb7dSmatthias.ringwald             return;
93300d93d79Smatthias.ringwald 
93400d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
93500d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
9362b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
9372b83fb7dSmatthias.ringwald             return;
93800d93d79Smatthias.ringwald         }
93900d93d79Smatthias.ringwald 
94000d93d79Smatthias.ringwald         default:
94100d93d79Smatthias.ringwald             break;
94200d93d79Smatthias.ringwald     }
94300d93d79Smatthias.ringwald 
94400d93d79Smatthias.ringwald 
94500d93d79Smatthias.ringwald     // Get potential destination CID
94600d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
94700d93d79Smatthias.ringwald 
94800d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
94900d93d79Smatthias.ringwald     linked_item_t *it;
95000d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
95100d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
95200d93d79Smatthias.ringwald         if (channel->handle == handle) {
95300d93d79Smatthias.ringwald             if (code & 1) {
954b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
955b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
95600d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9574e32727eSmatthias.ringwald                     break;
95800d93d79Smatthias.ringwald                 }
95900d93d79Smatthias.ringwald             } else {
960b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
96100d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
96200d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
9634e32727eSmatthias.ringwald                     break;
96400d93d79Smatthias.ringwald                 }
96500d93d79Smatthias.ringwald             }
96600d93d79Smatthias.ringwald         }
96700d93d79Smatthias.ringwald     }
96800d93d79Smatthias.ringwald }
96900d93d79Smatthias.ringwald 
97000d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
97100d93d79Smatthias.ringwald 
97200d93d79Smatthias.ringwald     // Get Channel ID
97300d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
97400d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
97500d93d79Smatthias.ringwald 
9765652b5ffS[email protected]     switch (channel_id) {
9775652b5ffS[email protected] 
9785652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
9795652b5ffS[email protected] 
98000d93d79Smatthias.ringwald             uint16_t command_offset = 8;
98100d93d79Smatthias.ringwald             while (command_offset < size) {
98200d93d79Smatthias.ringwald 
98300d93d79Smatthias.ringwald                 // handle signaling commands
98400d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
98500d93d79Smatthias.ringwald 
98600d93d79Smatthias.ringwald                 // increment command_offset
98700d93d79Smatthias.ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
98800d93d79Smatthias.ringwald             }
9895652b5ffS[email protected]             break;
99000d93d79Smatthias.ringwald         }
99100d93d79Smatthias.ringwald 
9925652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
9935652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
9945652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
9955652b5ffS[email protected]             }
9965652b5ffS[email protected]             break;
9975652b5ffS[email protected] 
9985652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
9995652b5ffS[email protected]             if (security_protocol_packet_handler) {
10005652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
10015652b5ffS[email protected]             }
10025652b5ffS[email protected]             break;
10035652b5ffS[email protected] 
10045652b5ffS[email protected]         default: {
100500d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
100600d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
100700d93d79Smatthias.ringwald             if (channel) {
100858de5610Smatthias.ringwald                 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
100900d93d79Smatthias.ringwald             }
10105652b5ffS[email protected]             break;
10115652b5ffS[email protected]         }
10125652b5ffS[email protected]     }
10135652b5ffS[email protected] 
10145652b5ffS[email protected]     l2cap_run();
101500d93d79Smatthias.ringwald }
101600d93d79Smatthias.ringwald 
10172718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
10182718e2e7Smatthias.ringwald     switch (packet_type) {
10192718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
10202718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
10212718e2e7Smatthias.ringwald             break;
10222718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
10232718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
10242718e2e7Smatthias.ringwald             break;
10252718e2e7Smatthias.ringwald         default:
10262718e2e7Smatthias.ringwald             break;
10272718e2e7Smatthias.ringwald     }
10282718e2e7Smatthias.ringwald }
102900d93d79Smatthias.ringwald 
103015ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
103127a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1032f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1033f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1034f62db1e3Smatthias.ringwald     // discard channel
1035f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1036d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1037c8e4258aSmatthias.ringwald }
10381e6aba47Smatthias.ringwald 
10399d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1040c52bf64dSmatthias.ringwald     linked_item_t *it;
10419d9bbc01Smatthias.ringwald 
10429d9bbc01Smatthias.ringwald     // close open channels
10439d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
10449d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
10459d9bbc01Smatthias.ringwald         if ( service->psm == psm){
10469d9bbc01Smatthias.ringwald             return service;
10479d9bbc01Smatthias.ringwald         };
10489d9bbc01Smatthias.ringwald     }
10499d9bbc01Smatthias.ringwald     return NULL;
10509d9bbc01Smatthias.ringwald }
10519d9bbc01Smatthias.ringwald 
105236944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
10534bb582b6Smatthias.ringwald     // check for alread registered psm
10544bb582b6Smatthias.ringwald     // TODO: emit error event
10559d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1056277abc2cSmatthias.ringwald     if (service) {
1057277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
105881476041Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
1059277abc2cSmatthias.ringwald         return;
1060277abc2cSmatthias.ringwald     }
10619d9bbc01Smatthias.ringwald 
10624bb582b6Smatthias.ringwald     // alloc structure
10634bb582b6Smatthias.ringwald     // TODO: emit error event
106428ca2b46S[email protected]     service = (l2cap_service_t *) btstack_memory_l2cap_service_get();
1065277abc2cSmatthias.ringwald     if (!service) {
1066277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
10675842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
1068277abc2cSmatthias.ringwald         return;
1069277abc2cSmatthias.ringwald     }
10709d9bbc01Smatthias.ringwald 
10719d9bbc01Smatthias.ringwald     // fill in
10729d9bbc01Smatthias.ringwald     service->psm = psm;
10739d9bbc01Smatthias.ringwald     service->mtu = mtu;
10749d9bbc01Smatthias.ringwald     service->connection = connection;
1075d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
10769d9bbc01Smatthias.ringwald 
10779d9bbc01Smatthias.ringwald     // add to services list
10789d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
1079c0e866bfSmatthias.ringwald 
1080c0e866bfSmatthias.ringwald     // enable page scan
1081c0e866bfSmatthias.ringwald     hci_connectable_control(1);
10825842b6d9Smatthias.ringwald 
10835842b6d9Smatthias.ringwald     // done
10845842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
10859d9bbc01Smatthias.ringwald }
10869d9bbc01Smatthias.ringwald 
108736944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
10889d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1089037d6e48Smatthias.ringwald     if (!service) return;
10909d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1091d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1092c0e866bfSmatthias.ringwald 
1093c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1094c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1095c0e866bfSmatthias.ringwald     hci_connectable_control(0);
10969d9bbc01Smatthias.ringwald }
10979d9bbc01Smatthias.ringwald 
10989d9bbc01Smatthias.ringwald //
109936944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
11009d9bbc01Smatthias.ringwald     linked_item_t *it;
11019d9bbc01Smatthias.ringwald 
11023a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1103c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1104c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1105c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1106c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1107cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1108c52bf64dSmatthias.ringwald         }
1109c52bf64dSmatthias.ringwald     }
11109d9bbc01Smatthias.ringwald 
1111645658c9Smatthias.ringwald     // unregister services
111269025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
111369025de8Smatthias.ringwald     while (it->next) {
111469025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1115645658c9Smatthias.ringwald         if (service->connection == connection){
111669025de8Smatthias.ringwald             it->next = it->next->next;
1117d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
11188149d2c7Smatthias.ringwald         } else {
11198149d2c7Smatthias.ringwald             it = it->next;
1120645658c9Smatthias.ringwald         }
1121645658c9Smatthias.ringwald     }
1122cb8eb7e6Smatthias.ringwald 
1123cb8eb7e6Smatthias.ringwald     // process
1124cb8eb7e6Smatthias.ringwald     l2cap_run();
1125c52bf64dSmatthias.ringwald }
11265652b5ffS[email protected] 
11275652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
11285652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
11295652b5ffS[email protected]     switch(channel_id){
11305652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
11315652b5ffS[email protected]             attribute_protocol_packet_handler = packet_handler;
11325652b5ffS[email protected]             break;
11335652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
11345652b5ffS[email protected]             security_protocol_packet_handler = packet_handler;
11355652b5ffS[email protected]             break;
11365652b5ffS[email protected]     }
11375652b5ffS[email protected] }
11385652b5ffS[email protected] 
1139