xref: /btstack/src/l2cap.c (revision ad67156049bb89d399216ad8ea5b1a0d0d231258)
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) {
126c9dc710bS[email protected]     log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u",
127e0abb8e7S[email protected]              status, bd_addr_to_str(channel->address), channel->handle, channel->psm,
128c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
129c9dc710bS[email protected]     uint8_t event[23];
13058de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
13158de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
13258de5610Smatthias.ringwald     event[2] = status;
13358de5610Smatthias.ringwald     bt_flip_addr(&event[3], channel->address);
13458de5610Smatthias.ringwald     bt_store_16(event,  9, channel->handle);
13558de5610Smatthias.ringwald     bt_store_16(event, 11, channel->psm);
13658de5610Smatthias.ringwald     bt_store_16(event, 13, channel->local_cid);
13758de5610Smatthias.ringwald     bt_store_16(event, 15, channel->remote_cid);
1384c98aa43Smatthias.ringwald     bt_store_16(event, 17, channel->local_mtu);
1394c98aa43Smatthias.ringwald     bt_store_16(event, 19, channel->remote_mtu);
140c9dc710bS[email protected]     bt_store_16(event, 19, channel->flush_timeout);
14158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
14258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
14358de5610Smatthias.ringwald }
14458de5610Smatthias.ringwald 
14558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
146e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
14758de5610Smatthias.ringwald     uint8_t event[4];
14858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
14958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
15058de5610Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
15158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
15258de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
15358de5610Smatthias.ringwald }
15458de5610Smatthias.ringwald 
15558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
156e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
157e0abb8e7S[email protected]              bd_addr_to_str(channel->address), channel->handle,  channel->psm, channel->local_cid, channel->remote_cid);
15858de5610Smatthias.ringwald     uint8_t event[16];
15958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
16058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
16158de5610Smatthias.ringwald     bt_flip_addr(&event[2], channel->address);
16258de5610Smatthias.ringwald     bt_store_16(event,  8, channel->handle);
16358de5610Smatthias.ringwald     bt_store_16(event, 10, channel->psm);
16458de5610Smatthias.ringwald     bt_store_16(event, 12, channel->local_cid);
16558de5610Smatthias.ringwald     bt_store_16(event, 14, channel->remote_cid);
16658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
16758de5610Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1680af41d30Smatthias.ringwald }
169808a48abSmatthias.ringwald 
1705842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
171e0abb8e7S[email protected]     log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm);
1725842b6d9Smatthias.ringwald     uint8_t event[5];
1735842b6d9Smatthias.ringwald     event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
1745842b6d9Smatthias.ringwald     event[1] = sizeof(event) - 2;
1755842b6d9Smatthias.ringwald     event[2] = status;
1765842b6d9Smatthias.ringwald     bt_store_16(event, 3, psm);
1775842b6d9Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1785842b6d9Smatthias.ringwald     (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
1795842b6d9Smatthias.ringwald }
1805842b6d9Smatthias.ringwald 
1816218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
182e0abb8e7S[email protected] 
183e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits);
1846218e6f1Smatthias.ringwald     // track credits
1856218e6f1Smatthias.ringwald     channel->packets_granted += credits;
1866218e6f1Smatthias.ringwald 
1876218e6f1Smatthias.ringwald     uint8_t event[5];
1886218e6f1Smatthias.ringwald     event[0] = L2CAP_EVENT_CREDITS;
1896218e6f1Smatthias.ringwald     event[1] = sizeof(event) - 2;
1906218e6f1Smatthias.ringwald     bt_store_16(event, 2, channel->local_cid);
1916218e6f1Smatthias.ringwald     event[4] = credits;
1926218e6f1Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1936218e6f1Smatthias.ringwald     l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
1946218e6f1Smatthias.ringwald }
1956218e6f1Smatthias.ringwald 
196808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){
197808a48abSmatthias.ringwald     new_credits_blocked = blocked;
198808a48abSmatthias.ringwald }
199808a48abSmatthias.ringwald 
20040d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){
201808a48abSmatthias.ringwald 
202808a48abSmatthias.ringwald     if (new_credits_blocked) return;    // we're told not to. used by daemon
203808a48abSmatthias.ringwald 
2048d371091Smatthias.ringwald     linked_item_t *it;
2058d371091Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2068d371091Smatthias.ringwald         if (!hci_number_free_acl_slots()) return;
2078d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
2080e7bc007Smatthias.ringwald         if (channel->state != L2CAP_STATE_OPEN) continue;
2094c744e21Smatthias.ringwald         if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
2108d371091Smatthias.ringwald             l2cap_emit_credits(channel, 1);
2118d371091Smatthias.ringwald         }
2128d371091Smatthias.ringwald     }
2138d371091Smatthias.ringwald }
2148d371091Smatthias.ringwald 
215b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
216f62db1e3Smatthias.ringwald     linked_item_t *it;
217f62db1e3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2188d371091Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
219b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
220f62db1e3Smatthias.ringwald             return channel;
221f62db1e3Smatthias.ringwald         }
222f62db1e3Smatthias.ringwald     }
223f62db1e3Smatthias.ringwald     return NULL;
224f62db1e3Smatthias.ringwald }
225f62db1e3Smatthias.ringwald 
2266b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2276b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2286b1fde37Smatthias.ringwald     if (!channel) return 0;
2296b1fde37Smatthias.ringwald     if (!channel->packets_granted) return 0;
2306b1fde37Smatthias.ringwald     return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
2316b1fde37Smatthias.ringwald }
2326b1fde37Smatthias.ringwald 
23396cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
23496cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
23596cbd662Smatthias.ringwald     if (channel) {
23696cbd662Smatthias.ringwald         return channel->remote_mtu;
23796cbd662Smatthias.ringwald     }
23896cbd662Smatthias.ringwald     return 0;
23996cbd662Smatthias.ringwald }
24096cbd662Smatthias.ringwald 
2415932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){
2425932bd7cS[email protected]     linked_item_t *it;
2435932bd7cS[email protected]     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
2445932bd7cS[email protected]         l2cap_channel_t * channel = (l2cap_channel_t *) it;
2455932bd7cS[email protected]         if ( &channel->rtx == ts) {
2465932bd7cS[email protected]             return channel;
2475932bd7cS[email protected]         }
2485932bd7cS[email protected]     }
2495932bd7cS[email protected]     return NULL;
2505932bd7cS[email protected] }
2515932bd7cS[email protected] 
2525932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){
2535932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
2545932bd7cS[email protected]     if (!ts) return;
2555932bd7cS[email protected] 
2565932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
2575932bd7cS[email protected] 
2585932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
2595932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
2605932bd7cS[email protected]     // notify client
2615932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
2625932bd7cS[email protected] 
2635932bd7cS[email protected]     // discard channel
2645932bd7cS[email protected]     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
2655932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
2665932bd7cS[email protected] }
2675932bd7cS[email protected] 
2685932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
2695932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
2705932bd7cS[email protected]     run_loop_remove_timer(&channel->rtx);
2715932bd7cS[email protected] }
2725932bd7cS[email protected] 
2735932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
2745932bd7cS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
2755932bd7cS[email protected]     l2cap_stop_rtx(channel);
2765932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
2775932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
2785932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
2795932bd7cS[email protected] }
2805932bd7cS[email protected] 
2815932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
2825932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
2835932bd7cS[email protected]     l2cap_stop_rtx(channel);
2845932bd7cS[email protected]     run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
2855932bd7cS[email protected]     run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
2865932bd7cS[email protected]     run_loop_add_timer(&channel->rtx);
2875932bd7cS[email protected] }
2885932bd7cS[email protected] 
2895932bd7cS[email protected] 
29058de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
291b1d43497Smatthias.ringwald 
292b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
293b1d43497Smatthias.ringwald         log_info("l2cap_send_signaling_packet, cannot send\n");
294b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
295b1d43497Smatthias.ringwald     }
296b1d43497Smatthias.ringwald 
2977b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
298b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
29958de5610Smatthias.ringwald     va_list argptr;
30058de5610Smatthias.ringwald     va_start(argptr, identifier);
301816c0598Smatthias.ringwald     uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
30258de5610Smatthias.ringwald     va_end(argptr);
3037b5fbe1fSmatthias.ringwald     // log_info("l2cap_send_signaling_packet con %u!\n", handle);
304816c0598Smatthias.ringwald     return hci_send_acl_packet(acl_buffer, len);
30558de5610Smatthias.ringwald }
30658de5610Smatthias.ringwald 
307b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
308b1d43497Smatthias.ringwald     return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
309b1d43497Smatthias.ringwald }
3106218e6f1Smatthias.ringwald 
311b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
312b1d43497Smatthias.ringwald 
313b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
314e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid);
315808a48abSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3168ea03fa5Smatthias.ringwald     }
3176218e6f1Smatthias.ringwald 
31858de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
319b1d43497Smatthias.ringwald     if (!channel) {
320e0abb8e7S[email protected]         log_error("l2cap_send_internal no channel for cid 0x%02x\n", local_cid);
321b1d43497Smatthias.ringwald         return -1;   // TODO: define error
3226218e6f1Smatthias.ringwald     }
3236218e6f1Smatthias.ringwald 
324b1d43497Smatthias.ringwald     if (channel->packets_granted == 0){
325e0abb8e7S[email protected]         log_error("l2cap_send_internal cid 0x%02x, no credits!\n", local_cid);
326b1d43497Smatthias.ringwald         return -1;  // TODO: define error
327b1d43497Smatthias.ringwald     }
328b1d43497Smatthias.ringwald 
329b1d43497Smatthias.ringwald     --channel->packets_granted;
330b1d43497Smatthias.ringwald 
331e0abb8e7S[email protected]     log_debug("l2cap_send_internal cid 0x%02x, handle %u, 1 credit used, credits left %u;\n",
332b1d43497Smatthias.ringwald                   local_cid, channel->handle, channel->packets_granted);
333b1d43497Smatthias.ringwald 
334b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
335b1d43497Smatthias.ringwald 
33658de5610Smatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
33758de5610Smatthias.ringwald     bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
33858de5610Smatthias.ringwald     // 2 - ACL length
33958de5610Smatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
34058de5610Smatthias.ringwald     // 4 - L2CAP packet length
34158de5610Smatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
34258de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
34358de5610Smatthias.ringwald     bt_store_16(acl_buffer, 6, channel->remote_cid);
34458de5610Smatthias.ringwald     // send
345b1d43497Smatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
34691b99603Smatthias.ringwald 
34791b99603Smatthias.ringwald     l2cap_hand_out_credits();
34891b99603Smatthias.ringwald 
3496218e6f1Smatthias.ringwald     return err;
35058de5610Smatthias.ringwald }
35158de5610Smatthias.ringwald 
3522149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
3532149f12eSmatthias.ringwald 
3542149f12eSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
355e0abb8e7S[email protected]         log_info("l2cap_send_prepared_to_handle cid 0x%02x, cannot send\n", cid);
3562149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3572149f12eSmatthias.ringwald     }
3582149f12eSmatthias.ringwald 
359e0abb8e7S[email protected]     log_debug("l2cap_send_prepared_to_handle cid 0x%02x, handle %u\n", cid, handle);
3602149f12eSmatthias.ringwald 
3612149f12eSmatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
3622149f12eSmatthias.ringwald 
3632149f12eSmatthias.ringwald     // 0 - Connection handle : PB=10 : BC=00
3642149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14));
3652149f12eSmatthias.ringwald     // 2 - ACL length
3662149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 2,  len + 4);
3672149f12eSmatthias.ringwald     // 4 - L2CAP packet length
3682149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 4,  len + 0);
3692149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
3702149f12eSmatthias.ringwald     bt_store_16(acl_buffer, 6, cid);
3712149f12eSmatthias.ringwald     // send
3722149f12eSmatthias.ringwald     int err = hci_send_acl_packet(acl_buffer, len+8);
3732149f12eSmatthias.ringwald 
3742149f12eSmatthias.ringwald     l2cap_hand_out_credits();
3752149f12eSmatthias.ringwald 
3762149f12eSmatthias.ringwald     return err;
3772149f12eSmatthias.ringwald }
3782149f12eSmatthias.ringwald 
379b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
380b1d43497Smatthias.ringwald 
381b1d43497Smatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
382e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid);
383b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
384b1d43497Smatthias.ringwald     }
385b1d43497Smatthias.ringwald 
386b1d43497Smatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
387b1d43497Smatthias.ringwald 
388b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
389b1d43497Smatthias.ringwald 
390b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
391b1d43497Smatthias.ringwald }
392b1d43497Smatthias.ringwald 
3932149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
3942149f12eSmatthias.ringwald 
3952149f12eSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
396e0abb8e7S[email protected]         log_info("l2cap_send_internal cid 0x%02x, cannot send\n", cid);
3972149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
3982149f12eSmatthias.ringwald     }
3992149f12eSmatthias.ringwald 
4002149f12eSmatthias.ringwald     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
4012149f12eSmatthias.ringwald 
4022149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
4032149f12eSmatthias.ringwald 
4042149f12eSmatthias.ringwald     return l2cap_send_prepared_connectionless(handle, cid, len);
4052149f12eSmatthias.ringwald }
4062149f12eSmatthias.ringwald 
40728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
40828ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
40928ca2b46S[email protected] }
41028ca2b46S[email protected] 
41128ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
41228ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
41328ca2b46S[email protected] }
41428ca2b46S[email protected] 
41528ca2b46S[email protected] 
416b1d43497Smatthias.ringwald 
4178158c421Smatthias.ringwald // MARK: L2CAP_RUN
4182cd0be45Smatthias.ringwald // process outstanding signaling tasks
4192cd0be45Smatthias.ringwald void l2cap_run(void){
4202b83fb7dSmatthias.ringwald 
4212b83fb7dSmatthias.ringwald     // check pending signaling responses
4222b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
4232b83fb7dSmatthias.ringwald 
42402b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
4252b83fb7dSmatthias.ringwald 
4262b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
4272b83fb7dSmatthias.ringwald         uint8_t sig_id = signaling_responses[0].sig_id;
4282b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
42963a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
4302b83fb7dSmatthias.ringwald 
4312b83fb7dSmatthias.ringwald         switch (signaling_responses[0].code){
4322b360848Smatthias.ringwald             case CONNECTION_REQUEST:
4332b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
4342b360848Smatthias.ringwald                 break;
4352b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
4362b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
4372b83fb7dSmatthias.ringwald                 break;
4382b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
4393b0484b3S[email protected]                 switch (infoType){
4403b0484b3S[email protected]                     case 1: { // Connectionless MTU
4413b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
4423b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
4433b0484b3S[email protected]                         break;
4443b0484b3S[email protected]                     }
4453b0484b3S[email protected]                     case 2: { // Extended Features Supported
4463b0484b3S[email protected]                         // extended features request supported, only supporing fixed channel map
4473b0484b3S[email protected]                         uint32_t features = 0x80;
4483b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
4493b0484b3S[email protected]                         break;
4503b0484b3S[email protected]                     }
4513b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
4523b0484b3S[email protected]                         uint8_t map[8];
4533b0484b3S[email protected]                         memset(map, 0, 8);
4543b0484b3S[email protected]                         map[0] = 0x01;  // L2CAP Signaling Channel
4553b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
4563b0484b3S[email protected]                         break;
4573b0484b3S[email protected]                     }
4583b0484b3S[email protected]                     default:
4592b83fb7dSmatthias.ringwald                         // all other types are not supported
4602b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
4613b0484b3S[email protected]                         break;
4622b83fb7dSmatthias.ringwald                 }
4632b83fb7dSmatthias.ringwald                 break;
46463a7246aSmatthias.ringwald             case COMMAND_REJECT:
46563a7246aSmatthias.ringwald                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result);
46663a7246aSmatthias.ringwald                 break;
4672b83fb7dSmatthias.ringwald             default:
4682b83fb7dSmatthias.ringwald                 // should not happen
4692b83fb7dSmatthias.ringwald                 break;
4702b83fb7dSmatthias.ringwald         }
4712b83fb7dSmatthias.ringwald 
4722b83fb7dSmatthias.ringwald         // remove first item
4732b83fb7dSmatthias.ringwald         signaling_responses_pending--;
4742b83fb7dSmatthias.ringwald         int i;
4752b83fb7dSmatthias.ringwald         for (i=0; i < signaling_responses_pending; i++){
47676115249S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
4772b83fb7dSmatthias.ringwald         }
4782b83fb7dSmatthias.ringwald     }
4792b83fb7dSmatthias.ringwald 
480ae280e73Smatthias.ringwald     uint8_t  config_options[4];
4812cd0be45Smatthias.ringwald     linked_item_t *it;
482756102d3Smatthias.ringwald     linked_item_t *next;
483756102d3Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = next){
484756102d3Smatthias.ringwald         next = it->next;    // cache next item as current item might get freed
4852cd0be45Smatthias.ringwald 
48602b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
48702b22dc4Smatthias.ringwald         if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
4882cd0be45Smatthias.ringwald 
4892cd0be45Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
4906e6710ebSmatthias.ringwald 
4917b5fbe1fSmatthias.ringwald         // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
4926e6710ebSmatthias.ringwald 
49356081214Smatthias.ringwald 
4942cd0be45Smatthias.ringwald         switch (channel->state){
4952cd0be45Smatthias.ringwald 
496*ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
497*ad671560S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
498*ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
499*ad671560S[email protected]                     l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 1, 0);
500*ad671560S[email protected]                 }
501*ad671560S[email protected]                 break;
502*ad671560S[email protected] 
50302b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
50464472d52Smatthias.ringwald                 // send connection request - set state first
50564472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
50602b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
5078f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
50802b22dc4Smatthias.ringwald                 break;
50902b22dc4Smatthias.ringwald 
510e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
511b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
512e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
513756102d3Smatthias.ringwald                 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list
514d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
515e7ff783cSmatthias.ringwald                 break;
516e7ff783cSmatthias.ringwald 
517552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
518fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
51928ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
5202a544672Smatthias.ringwald                 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
521552d92a1Smatthias.ringwald                 break;
522552d92a1Smatthias.ringwald 
5236fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
5246fdcc387Smatthias.ringwald                 // success, start l2cap handshake
525b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
5266fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
5272a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
5285932bd7cS[email protected]                 l2cap_start_rtx(channel);
5296fdcc387Smatthias.ringwald                 break;
5306fdcc387Smatthias.ringwald 
531fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
53273cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
53363a7246aSmatthias.ringwald                     uint16_t flags = 0;
53428ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
53563a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
53663a7246aSmatthias.ringwald                         flags = 1;
53763a7246aSmatthias.ringwald                     } else {
53828ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
53963a7246aSmatthias.ringwald                     }
54063a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
54163a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
54263a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
54363a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
54463a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
54563a7246aSmatthias.ringwald                         bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
54663a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
54763a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
54863a7246aSmatthias.ringwald                     } else {
54963a7246aSmatthias.ringwald                         l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
55063a7246aSmatthias.ringwald                     }
55163a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
552fa8473a4Smatthias.ringwald                 }
55373cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
55428ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
55528ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
556b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
557ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
558ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
559ae280e73Smatthias.ringwald                     bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
560b1988dceSmatthias.ringwald                     l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
5615932bd7cS[email protected]                     l2cap_start_rtx(channel);
562fa8473a4Smatthias.ringwald                 }
563fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
564552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
565552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
566552d92a1Smatthias.ringwald                     l2cap_emit_credits(channel, 1);
567fa8473a4Smatthias.ringwald                 }
568552d92a1Smatthias.ringwald                 break;
569552d92a1Smatthias.ringwald 
570e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
571b1988dceSmatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
5725932bd7cS[email protected]                 // we don't start an RTX timer for a disconnect - there's no point in closing the channel if the other side doesn't respond :)
573756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
574e7ff783cSmatthias.ringwald                 break;
575e7ff783cSmatthias.ringwald 
576e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
577b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
5782cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
5792a544672Smatthias.ringwald                 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
5802cd0be45Smatthias.ringwald                 break;
5812cd0be45Smatthias.ringwald             default:
5822cd0be45Smatthias.ringwald                 break;
5832cd0be45Smatthias.ringwald         }
5842cd0be45Smatthias.ringwald     }
5852cd0be45Smatthias.ringwald }
5862cd0be45Smatthias.ringwald 
5874aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
588816c0598Smatthias.ringwald     return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
589fa8c92f6Smatthias.ringwald }
590fa8c92f6Smatthias.ringwald 
5911e6aba47Smatthias.ringwald // open outgoing L2CAP channel
59215470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
59315470d27Smatthias.ringwald                                    bd_addr_t address, uint16_t psm, uint16_t mtu){
5941e6aba47Smatthias.ringwald 
595e0abb8e7S[email protected]     log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
596e0abb8e7S[email protected] 
5971e6aba47Smatthias.ringwald     // alloc structure
59828ca2b46S[email protected]     l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
5992b360848Smatthias.ringwald     if (!chan) {
6002b360848Smatthias.ringwald         // emit error event
6012b360848Smatthias.ringwald         l2cap_channel_t dummy_channel;
6022b360848Smatthias.ringwald         BD_ADDR_COPY(dummy_channel.address, address);
6032b360848Smatthias.ringwald         dummy_channel.psm = psm;
6042b360848Smatthias.ringwald         l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
6052b360848Smatthias.ringwald         return;
6062b360848Smatthias.ringwald     }
6071d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
6082985cb84Smatthias.ringwald     if (mtu > l2cap_max_mtu()) {
6092985cb84Smatthias.ringwald         mtu = l2cap_max_mtu();
6109775e25bSmatthias.ringwald     }
6119775e25bSmatthias.ringwald 
6121e6aba47Smatthias.ringwald     // fill in
6131e6aba47Smatthias.ringwald     BD_ADDR_COPY(chan->address, address);
6141e6aba47Smatthias.ringwald     chan->psm = psm;
6151e6aba47Smatthias.ringwald     chan->handle = 0;
6161e6aba47Smatthias.ringwald     chan->connection = connection;
6176b296a27Smatthias.ringwald     chan->packet_handler = packet_handler;
6182784b77dSmatthias.ringwald     chan->remote_mtu = L2CAP_MINIMAL_MTU;
61915470d27Smatthias.ringwald     chan->local_mtu = mtu;
6206218e6f1Smatthias.ringwald     chan->packets_granted = 0;
6216218e6f1Smatthias.ringwald 
6221e6aba47Smatthias.ringwald     // set initial state
62302b22dc4Smatthias.ringwald     chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
62473cf2b3dSmatthias.ringwald     chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
625b1988dceSmatthias.ringwald     chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
626b1988dceSmatthias.ringwald     chan->local_sig_id = L2CAP_SIG_ID_INVALID;
6271e6aba47Smatthias.ringwald 
6281e6aba47Smatthias.ringwald     // add to connections list
6291e6aba47Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) chan);
6301e6aba47Smatthias.ringwald 
63102b22dc4Smatthias.ringwald     l2cap_run();
63243625864Smatthias.ringwald }
63343625864Smatthias.ringwald 
634b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
635e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
636b35f641cSmatthias.ringwald     // find channel for local_cid
637b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
638f62db1e3Smatthias.ringwald     if (channel) {
639e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
640f62db1e3Smatthias.ringwald     }
6412cd0be45Smatthias.ringwald     // process
6422cd0be45Smatthias.ringwald     l2cap_run();
64343625864Smatthias.ringwald }
6441e6aba47Smatthias.ringwald 
645afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
64615ec09bbSmatthias.ringwald     linked_item_t *it = (linked_item_t *) &l2cap_channels;
64715ec09bbSmatthias.ringwald     while (it->next){
64815ec09bbSmatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
649b448a0e7Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
65002b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
651afde0c52Smatthias.ringwald                 // failure, forward error code
652afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
653afde0c52Smatthias.ringwald                 // discard channel
65415ec09bbSmatthias.ringwald                 it->next = it->next->next;
655d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
656afde0c52Smatthias.ringwald             }
65715ec09bbSmatthias.ringwald         } else {
65815ec09bbSmatthias.ringwald             it = it->next;
659afde0c52Smatthias.ringwald         }
660afde0c52Smatthias.ringwald     }
661afde0c52Smatthias.ringwald }
662afde0c52Smatthias.ringwald 
663afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
664afde0c52Smatthias.ringwald     linked_item_t *it;
665afde0c52Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
666afde0c52Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
667afde0c52Smatthias.ringwald         if ( ! BD_ADDR_CMP( channel->address, address) ){
66802b22dc4Smatthias.ringwald             if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
669b448a0e7Smatthias.ringwald                 // success, start l2cap handshake
6706fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
671afde0c52Smatthias.ringwald                 channel->handle = handle;
672b35f641cSmatthias.ringwald                 channel->local_cid = l2cap_next_local_cid();
673afde0c52Smatthias.ringwald             }
674afde0c52Smatthias.ringwald         }
675afde0c52Smatthias.ringwald     }
6766fdcc387Smatthias.ringwald     // process
6776fdcc387Smatthias.ringwald     l2cap_run();
678afde0c52Smatthias.ringwald }
679b448a0e7Smatthias.ringwald 
680afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){
681afde0c52Smatthias.ringwald 
682afde0c52Smatthias.ringwald     bd_addr_t address;
683afde0c52Smatthias.ringwald     hci_con_handle_t handle;
6846e6710ebSmatthias.ringwald     l2cap_channel_t * channel;
68536944dffSmatthias.ringwald     linked_item_t *it;
6862d00edd4Smatthias.ringwald     int hci_con_used;
687afde0c52Smatthias.ringwald 
688afde0c52Smatthias.ringwald     switch(packet[0]){
689afde0c52Smatthias.ringwald 
690afde0c52Smatthias.ringwald         // handle connection complete events
691afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
692afde0c52Smatthias.ringwald             bt_flip_addr(address, &packet[5]);
693afde0c52Smatthias.ringwald             if (packet[2] == 0){
694afde0c52Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
695afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
696afde0c52Smatthias.ringwald             } else {
697afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
698afde0c52Smatthias.ringwald             }
699afde0c52Smatthias.ringwald             break;
700afde0c52Smatthias.ringwald 
701afde0c52Smatthias.ringwald         // handle successful create connection cancel command
702afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
703afde0c52Smatthias.ringwald             if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
704afde0c52Smatthias.ringwald                 if (packet[5] == 0){
705afde0c52Smatthias.ringwald                     bt_flip_addr(address, &packet[6]);
706afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
707afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
70803cfbabcSmatthias.ringwald                 }
7091e6aba47Smatthias.ringwald             }
71039d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
71139d59809Smatthias.ringwald             break;
71239d59809Smatthias.ringwald 
71339d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
71439d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
715afde0c52Smatthias.ringwald             break;
71627a923d0Smatthias.ringwald 
7171e6aba47Smatthias.ringwald         // handle disconnection complete events
718afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
71927a923d0Smatthias.ringwald             // send l2cap disconnect events for all channels on this handle
720afde0c52Smatthias.ringwald             handle = READ_BT_16(packet, 3);
72115ec09bbSmatthias.ringwald             it = (linked_item_t *) &l2cap_channels;
72215ec09bbSmatthias.ringwald             while (it->next){
72339ce35a7Smatthias.ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
72427a923d0Smatthias.ringwald                 if ( channel->handle == handle ){
72515ec09bbSmatthias.ringwald                     // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
7262060cf14Smatthias.ringwald                     it->next = it->next->next;
72715ec09bbSmatthias.ringwald                     l2cap_emit_channel_closed(channel);
728d3a9df87Smatthias.ringwald                     btstack_memory_l2cap_channel_free(channel);
72915ec09bbSmatthias.ringwald                 } else {
73015ec09bbSmatthias.ringwald                     it = it->next;
73127a923d0Smatthias.ringwald                 }
73227a923d0Smatthias.ringwald             }
733afde0c52Smatthias.ringwald             break;
734fcadd0caSmatthias.ringwald 
7356218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
73602b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
7378d371091Smatthias.ringwald             l2cap_hand_out_credits();
7386218e6f1Smatthias.ringwald             break;
7396218e6f1Smatthias.ringwald 
740ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
741afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
74236944dffSmatthias.ringwald             handle = READ_BT_16(packet, 2);
74380ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
7442d00edd4Smatthias.ringwald             hci_con_used = 0;
745ee091cf1Smatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
746ee091cf1Smatthias.ringwald                 channel = (l2cap_channel_t *) it;
747ee091cf1Smatthias.ringwald                 if (channel->handle == handle) {
7482d00edd4Smatthias.ringwald                     hci_con_used = 1;
749ee091cf1Smatthias.ringwald                 }
750ee091cf1Smatthias.ringwald             }
7512d00edd4Smatthias.ringwald             if (hci_con_used) break;
75232ab9390Smatthias.ringwald             if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
7539edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
754afde0c52Smatthias.ringwald             break;
755ee091cf1Smatthias.ringwald 
7566e6710ebSmatthias.ringwald         case DAEMON_EVENT_HCI_PACKET_SENT:
7576e6710ebSmatthias.ringwald             for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
7586e6710ebSmatthias.ringwald                 channel = (l2cap_channel_t *) it;
7596e6710ebSmatthias.ringwald                 if (channel->packet_handler) {
7606e6710ebSmatthias.ringwald                     (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
7616e6710ebSmatthias.ringwald                 }
7626e6710ebSmatthias.ringwald             }
7635652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
7645652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
7655652b5ffS[email protected]             }
7665652b5ffS[email protected]             if (security_protocol_packet_handler) {
767133efcfdSmatthias.ringwald                 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
7685652b5ffS[email protected]             }
7696e6710ebSmatthias.ringwald             break;
7706e6710ebSmatthias.ringwald 
771afde0c52Smatthias.ringwald         default:
772afde0c52Smatthias.ringwald             break;
773afde0c52Smatthias.ringwald     }
774afde0c52Smatthias.ringwald 
775afde0c52Smatthias.ringwald     // pass on
776b502e1b0Smatthias.ringwald     (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
7771e6aba47Smatthias.ringwald }
7781e6aba47Smatthias.ringwald 
779afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
780b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
781e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
782e7ff783cSmatthias.ringwald     l2cap_run();
78384836b65Smatthias.ringwald }
78484836b65Smatthias.ringwald 
7852b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
7864cf56b4aSmatthias.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."
7872b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
7882b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
7892b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
7902b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
7912b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
7922b360848Smatthias.ringwald         signaling_responses_pending++;
7932b360848Smatthias.ringwald         l2cap_run();
7942b360848Smatthias.ringwald     }
7952b360848Smatthias.ringwald }
7962b360848Smatthias.ringwald 
797b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
798645658c9Smatthias.ringwald 
799e0abb8e7S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x\n", handle, psm, source_cid);
800645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
801645658c9Smatthias.ringwald     if (!service) {
802645658c9Smatthias.ringwald         // 0x0002 PSM not supported
8032b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
804645658c9Smatthias.ringwald         return;
805645658c9Smatthias.ringwald     }
806645658c9Smatthias.ringwald 
8075061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
808645658c9Smatthias.ringwald     if (!hci_connection) {
8092b360848Smatthias.ringwald         //
8107d67539fSmatthias.ringwald         log_error("no hci_connection for handle %u\n", handle);
811645658c9Smatthias.ringwald         return;
812645658c9Smatthias.ringwald     }
813645658c9Smatthias.ringwald     // alloc structure
8147b5fbe1fSmatthias.ringwald     // log_info("l2cap_handle_connection_request register channel\n");
81528ca2b46S[email protected]     l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
8162b360848Smatthias.ringwald     if (!channel){
8172b360848Smatthias.ringwald         // 0x0004 No resources available
8182b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
8192b360848Smatthias.ringwald         return;
8202b360848Smatthias.ringwald     }
821645658c9Smatthias.ringwald 
822645658c9Smatthias.ringwald     // fill in
823169f8b28Smatthias.ringwald     BD_ADDR_COPY(channel->address, hci_connection->address);
824169f8b28Smatthias.ringwald     channel->psm = psm;
825169f8b28Smatthias.ringwald     channel->handle = handle;
826169f8b28Smatthias.ringwald     channel->connection = service->connection;
827f8dd2f72Smatthias.ringwald     channel->packet_handler = service->packet_handler;
828b35f641cSmatthias.ringwald     channel->local_cid  = l2cap_next_local_cid();
829b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
830fa2b2627Smatthias.ringwald     channel->local_mtu  = service->mtu;
8310a18a8e9Smatthias.ringwald     channel->remote_mtu = L2CAP_DEFAULT_MTU;
832761b0451Smatthias.ringwald     channel->packets_granted = 0;
833b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
834645658c9Smatthias.ringwald 
8351d279b20Smatthias.ringwald     // limit local mtu to max acl packet length
8362985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
8372985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
8389775e25bSmatthias.ringwald     }
8399775e25bSmatthias.ringwald 
840645658c9Smatthias.ringwald     // set initial state
841*ad671560S[email protected]     channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
842*ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
843e405ae81Smatthias.ringwald 
844645658c9Smatthias.ringwald     // add to connections list
845169f8b28Smatthias.ringwald     linked_list_add(&l2cap_channels, (linked_item_t *) channel);
846645658c9Smatthias.ringwald 
847*ad671560S[email protected]     //
848*ad671560S[email protected]     // gap_security_level_t current_level  = gap_security_level(handle);
849*ad671560S[email protected]     // gap_security_level_t required_level = LEVEL_2;
850*ad671560S[email protected]     // if (current_level < required_level){
851*ad671560S[email protected]     //     gap_request_security_level(handle, required_level);
852*ad671560S[email protected]     //     return;
853*ad671560S[email protected]     // }
854*ad671560S[email protected] 
855e405ae81Smatthias.ringwald     // emit incoming connection request
856e405ae81Smatthias.ringwald     l2cap_emit_connection_request(channel);
857e405ae81Smatthias.ringwald }
858645658c9Smatthias.ringwald 
859b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){
860e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
861b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
862e405ae81Smatthias.ringwald     if (!channel) {
8637d67539fSmatthias.ringwald         log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
864e405ae81Smatthias.ringwald         return;
865e405ae81Smatthias.ringwald     }
866e405ae81Smatthias.ringwald 
867552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
868e405ae81Smatthias.ringwald 
869552d92a1Smatthias.ringwald     // process
870552d92a1Smatthias.ringwald     l2cap_run();
871e405ae81Smatthias.ringwald }
872645658c9Smatthias.ringwald 
873b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
874e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
875b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
876e405ae81Smatthias.ringwald     if (!channel) {
8777d67539fSmatthias.ringwald         log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
878e405ae81Smatthias.ringwald         return;
879e405ae81Smatthias.ringwald     }
880e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
881e7ff783cSmatthias.ringwald     channel->reason = reason;
882e7ff783cSmatthias.ringwald     l2cap_run();
883645658c9Smatthias.ringwald }
884645658c9Smatthias.ringwald 
8852784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
886b1988dceSmatthias.ringwald 
887b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
888b1988dceSmatthias.ringwald 
88963a7246aSmatthias.ringwald     uint16_t flags = READ_BT_16(command, 6);
89063a7246aSmatthias.ringwald     if (flags & 1) {
89163a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
89263a7246aSmatthias.ringwald     }
89363a7246aSmatthias.ringwald 
8942784b77dSmatthias.ringwald     // accept the other's configuration options
8953de7c0caSmatthias.ringwald     uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
8963de7c0caSmatthias.ringwald     uint16_t pos     = 8;
8973de7c0caSmatthias.ringwald     while (pos < end_pos){
89863a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
89963a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
90063a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
90163a7246aSmatthias.ringwald         pos++;
9021dc511deSmatthias.ringwald         uint8_t length = command[pos++];
9031dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
90463a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
9051dc511deSmatthias.ringwald             channel->remote_mtu = READ_BT_16(command, pos);
906e0abb8e7S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
90763a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
90863a7246aSmatthias.ringwald         }
9090fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
9100fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
9110fe7a9d0S[email protected]             channel->flush_timeout = READ_BT_16(command, pos);
9120fe7a9d0S[email protected]         }
91363a7246aSmatthias.ringwald         // check for unknown options
91463a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
915c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
91663a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
9171dc511deSmatthias.ringwald         }
9181dc511deSmatthias.ringwald         pos += length;
9191dc511deSmatthias.ringwald     }
9202784b77dSmatthias.ringwald }
9212784b77dSmatthias.ringwald 
922fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
9237b5fbe1fSmatthias.ringwald     // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
92473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
92573cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
926fa8473a4Smatthias.ringwald     return 1;
927fa8473a4Smatthias.ringwald }
928fa8473a4Smatthias.ringwald 
929fa8473a4Smatthias.ringwald 
93000d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
9311e6aba47Smatthias.ringwald 
93200d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
93300d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
93438e5900eSmatthias.ringwald     uint16_t result = 0;
9351e6aba47Smatthias.ringwald 
9365842b6d9Smatthias.ringwald     log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
937b35f641cSmatthias.ringwald 
9389a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
9399a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
9409a011532Smatthias.ringwald         switch (channel->state){
941fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
9429a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
9432b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
9449a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
9459a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
9469a011532Smatthias.ringwald                 break;
9479a011532Smatthias.ringwald 
9489a011532Smatthias.ringwald             default:
9499a011532Smatthias.ringwald                 // ignore in other states
9509a011532Smatthias.ringwald                 break;
9519a011532Smatthias.ringwald         }
9529a011532Smatthias.ringwald         return;
9539a011532Smatthias.ringwald     }
9549a011532Smatthias.ringwald 
95556081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
9561e6aba47Smatthias.ringwald     switch (channel->state) {
9571e6aba47Smatthias.ringwald 
9581e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
9591e6aba47Smatthias.ringwald             switch (code){
9601e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
9615932bd7cS[email protected]                     l2cap_stop_rtx(channel);
96200d93d79Smatthias.ringwald                     result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
96338e5900eSmatthias.ringwald                     switch (result) {
96438e5900eSmatthias.ringwald                         case 0:
965169f8b28Smatthias.ringwald                             // successful connection
96600d93d79Smatthias.ringwald                             channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
967fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
96828ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
96938e5900eSmatthias.ringwald                             break;
97038e5900eSmatthias.ringwald                         case 1:
9715932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
9725932bd7cS[email protected]                             l2cap_start_ertx(channel);
97338e5900eSmatthias.ringwald                             break;
97438e5900eSmatthias.ringwald                         default:
975eb920dbeSmatthias.ringwald                             // channel closed
976eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
977f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
97838e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
979eb920dbeSmatthias.ringwald 
980eb920dbeSmatthias.ringwald                             // drop link key if security block
981eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
982eb920dbeSmatthias.ringwald                                 hci_drop_link_key_for_bd_addr(&channel->address);
983eb920dbeSmatthias.ringwald                             }
984eb920dbeSmatthias.ringwald 
985eb920dbeSmatthias.ringwald                             // discard channel
986eb920dbeSmatthias.ringwald                             linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
987d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
98838e5900eSmatthias.ringwald                             break;
9891e6aba47Smatthias.ringwald                     }
9901e6aba47Smatthias.ringwald                     break;
99138e5900eSmatthias.ringwald 
99238e5900eSmatthias.ringwald                 default:
9931e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
99438e5900eSmatthias.ringwald                     break;
9951e6aba47Smatthias.ringwald             }
9961e6aba47Smatthias.ringwald             break;
9971e6aba47Smatthias.ringwald 
998fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
999fe9d8984S[email protected]             result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1000ae280e73Smatthias.ringwald             switch (code) {
1001ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
100228ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1003ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
100463a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
100563a7246aSmatthias.ringwald                         // only done if continuation not set
100663a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
100763a7246aSmatthias.ringwald                     }
1008ae280e73Smatthias.ringwald                     break;
10091e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
10105932bd7cS[email protected]                     l2cap_stop_rtx(channel);
10115932bd7cS[email protected]                     switch (result){
10125932bd7cS[email protected]                         case 0: // success
10135932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
10145932bd7cS[email protected]                             break;
10155932bd7cS[email protected]                         case 4: // pending
10165932bd7cS[email protected]                             l2cap_start_ertx(channel);
10175932bd7cS[email protected]                             break;
10185932bd7cS[email protected]                         default:
1019fe9d8984S[email protected]                             // retry on negative result
1020fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1021fe9d8984S[email protected]                             break;
1022fe9d8984S[email protected]                     }
10235a67bd4aSmatthias.ringwald                     break;
10245a67bd4aSmatthias.ringwald                 default:
10255a67bd4aSmatthias.ringwald                     break;
10261e6aba47Smatthias.ringwald             }
1027fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1028fa8473a4Smatthias.ringwald                 // for open:
10295a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1030fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
10316218e6f1Smatthias.ringwald                 l2cap_emit_credits(channel, 1);
1032c8e4258aSmatthias.ringwald             }
1033c8e4258aSmatthias.ringwald             break;
1034f62db1e3Smatthias.ringwald 
1035f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1036f62db1e3Smatthias.ringwald             switch (code) {
1037f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
103827a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
103927a923d0Smatthias.ringwald                     break;
10405a67bd4aSmatthias.ringwald                 default:
10415a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
10425a67bd4aSmatthias.ringwald                     break;
104327a923d0Smatthias.ringwald             }
104427a923d0Smatthias.ringwald             break;
104584836b65Smatthias.ringwald 
104684836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
104784836b65Smatthias.ringwald             // @TODO handle incoming requests
104884836b65Smatthias.ringwald             break;
104984836b65Smatthias.ringwald 
105084836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
105184836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
105284836b65Smatthias.ringwald             break;
105310642e45Smatthias.ringwald         default:
105410642e45Smatthias.ringwald             break;
105527a923d0Smatthias.ringwald     }
10567b5fbe1fSmatthias.ringwald     // log_info("new state %u\n", channel->state);
105727a923d0Smatthias.ringwald }
105827a923d0Smatthias.ringwald 
105900d93d79Smatthias.ringwald 
106000d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
106100d93d79Smatthias.ringwald 
106200d93d79Smatthias.ringwald     // get code, signalind identifier and command len
106300d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
106400d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
106500d93d79Smatthias.ringwald 
106600d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
106700d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
106863a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
106900d93d79Smatthias.ringwald         return;
107000d93d79Smatthias.ringwald     }
107100d93d79Smatthias.ringwald 
107200d93d79Smatthias.ringwald     // general commands without an assigned channel
107300d93d79Smatthias.ringwald     switch(code) {
107400d93d79Smatthias.ringwald 
107500d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
107600d93d79Smatthias.ringwald             uint16_t psm =        READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
107700d93d79Smatthias.ringwald             uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
107800d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
10792b83fb7dSmatthias.ringwald             return;
108000d93d79Smatthias.ringwald         }
108100d93d79Smatthias.ringwald 
10822b360848Smatthias.ringwald         case ECHO_REQUEST:
10832b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
10842b83fb7dSmatthias.ringwald             return;
108500d93d79Smatthias.ringwald 
108600d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
108700d93d79Smatthias.ringwald             uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
10882b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
10892b83fb7dSmatthias.ringwald             return;
109000d93d79Smatthias.ringwald         }
109100d93d79Smatthias.ringwald 
109200d93d79Smatthias.ringwald         default:
109300d93d79Smatthias.ringwald             break;
109400d93d79Smatthias.ringwald     }
109500d93d79Smatthias.ringwald 
109600d93d79Smatthias.ringwald 
109700d93d79Smatthias.ringwald     // Get potential destination CID
109800d93d79Smatthias.ringwald     uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
109900d93d79Smatthias.ringwald 
110000d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
110100d93d79Smatthias.ringwald     linked_item_t *it;
110200d93d79Smatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
110300d93d79Smatthias.ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) it;
110400d93d79Smatthias.ringwald         if (channel->handle == handle) {
110500d93d79Smatthias.ringwald             if (code & 1) {
1106b1988dceSmatthias.ringwald                 // match odd commands (responses) by previous signaling identifier
1107b1988dceSmatthias.ringwald                 if (channel->local_sig_id == sig_id) {
110800d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
11094e32727eSmatthias.ringwald                     break;
111000d93d79Smatthias.ringwald                 }
111100d93d79Smatthias.ringwald             } else {
1112b1988dceSmatthias.ringwald                 // match even commands (requests) by local channel id
111300d93d79Smatthias.ringwald                 if (channel->local_cid == dest_cid) {
111400d93d79Smatthias.ringwald                     l2cap_signaling_handler_channel(channel, command);
11154e32727eSmatthias.ringwald                     break;
111600d93d79Smatthias.ringwald                 }
111700d93d79Smatthias.ringwald             }
111800d93d79Smatthias.ringwald         }
111900d93d79Smatthias.ringwald     }
112000d93d79Smatthias.ringwald }
112100d93d79Smatthias.ringwald 
112200d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
112300d93d79Smatthias.ringwald 
112400d93d79Smatthias.ringwald     // Get Channel ID
112500d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
112600d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
112700d93d79Smatthias.ringwald 
11285652b5ffS[email protected]     switch (channel_id) {
11295652b5ffS[email protected] 
11305652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
11315652b5ffS[email protected] 
113200d93d79Smatthias.ringwald             uint16_t command_offset = 8;
113300d93d79Smatthias.ringwald             while (command_offset < size) {
113400d93d79Smatthias.ringwald 
113500d93d79Smatthias.ringwald                 // handle signaling commands
113600d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
113700d93d79Smatthias.ringwald 
113800d93d79Smatthias.ringwald                 // increment command_offset
113900d93d79Smatthias.ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
114000d93d79Smatthias.ringwald             }
11415652b5ffS[email protected]             break;
114200d93d79Smatthias.ringwald         }
114300d93d79Smatthias.ringwald 
11445652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
11455652b5ffS[email protected]             if (attribute_protocol_packet_handler) {
11465652b5ffS[email protected]                 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
11475652b5ffS[email protected]             }
11485652b5ffS[email protected]             break;
11495652b5ffS[email protected] 
11505652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
11515652b5ffS[email protected]             if (security_protocol_packet_handler) {
11525652b5ffS[email protected]                 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
11535652b5ffS[email protected]             }
11545652b5ffS[email protected]             break;
11555652b5ffS[email protected] 
11565652b5ffS[email protected]         default: {
115700d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
115800d93d79Smatthias.ringwald             l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
115900d93d79Smatthias.ringwald             if (channel) {
116058de5610Smatthias.ringwald                 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
116100d93d79Smatthias.ringwald             }
11625652b5ffS[email protected]             break;
11635652b5ffS[email protected]         }
11645652b5ffS[email protected]     }
11655652b5ffS[email protected] 
11665652b5ffS[email protected]     l2cap_run();
116700d93d79Smatthias.ringwald }
116800d93d79Smatthias.ringwald 
11692718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
11702718e2e7Smatthias.ringwald     switch (packet_type) {
11712718e2e7Smatthias.ringwald         case HCI_EVENT_PACKET:
11722718e2e7Smatthias.ringwald             l2cap_event_handler(packet, size);
11732718e2e7Smatthias.ringwald             break;
11742718e2e7Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
11752718e2e7Smatthias.ringwald             l2cap_acl_handler(packet, size);
11762718e2e7Smatthias.ringwald             break;
11772718e2e7Smatthias.ringwald         default:
11782718e2e7Smatthias.ringwald             break;
11792718e2e7Smatthias.ringwald     }
11802718e2e7Smatthias.ringwald }
118100d93d79Smatthias.ringwald 
118215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
118327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){
1184f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1185f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1186f62db1e3Smatthias.ringwald     // discard channel
1187f62db1e3Smatthias.ringwald     linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
1188d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1189c8e4258aSmatthias.ringwald }
11901e6aba47Smatthias.ringwald 
11919d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){
1192c52bf64dSmatthias.ringwald     linked_item_t *it;
11939d9bbc01Smatthias.ringwald 
11949d9bbc01Smatthias.ringwald     // close open channels
11959d9bbc01Smatthias.ringwald     for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
11969d9bbc01Smatthias.ringwald         l2cap_service_t * service = ((l2cap_service_t *) it);
11979d9bbc01Smatthias.ringwald         if ( service->psm == psm){
11989d9bbc01Smatthias.ringwald             return service;
11999d9bbc01Smatthias.ringwald         };
12009d9bbc01Smatthias.ringwald     }
12019d9bbc01Smatthias.ringwald     return NULL;
12029d9bbc01Smatthias.ringwald }
12039d9bbc01Smatthias.ringwald 
120436944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
1205e0abb8e7S[email protected] 
1206e0abb8e7S[email protected]     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1207e0abb8e7S[email protected] 
12084bb582b6Smatthias.ringwald     // check for alread registered psm
12094bb582b6Smatthias.ringwald     // TODO: emit error event
12109d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1211277abc2cSmatthias.ringwald     if (service) {
1212277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
121381476041Smatthias.ringwald         l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
1214277abc2cSmatthias.ringwald         return;
1215277abc2cSmatthias.ringwald     }
12169d9bbc01Smatthias.ringwald 
12174bb582b6Smatthias.ringwald     // alloc structure
12184bb582b6Smatthias.ringwald     // TODO: emit error event
121928ca2b46S[email protected]     service = (l2cap_service_t *) btstack_memory_l2cap_service_get();
1220277abc2cSmatthias.ringwald     if (!service) {
1221277abc2cSmatthias.ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
12225842b6d9Smatthias.ringwald         l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
1223277abc2cSmatthias.ringwald         return;
1224277abc2cSmatthias.ringwald     }
12259d9bbc01Smatthias.ringwald 
12269d9bbc01Smatthias.ringwald     // fill in
12279d9bbc01Smatthias.ringwald     service->psm = psm;
12289d9bbc01Smatthias.ringwald     service->mtu = mtu;
12299d9bbc01Smatthias.ringwald     service->connection = connection;
1230d8497f19Smatthias.ringwald     service->packet_handler = packet_handler;
12319d9bbc01Smatthias.ringwald 
12329d9bbc01Smatthias.ringwald     // add to services list
12339d9bbc01Smatthias.ringwald     linked_list_add(&l2cap_services, (linked_item_t *) service);
1234c0e866bfSmatthias.ringwald 
1235c0e866bfSmatthias.ringwald     // enable page scan
1236c0e866bfSmatthias.ringwald     hci_connectable_control(1);
12375842b6d9Smatthias.ringwald 
12385842b6d9Smatthias.ringwald     // done
12395842b6d9Smatthias.ringwald     l2cap_emit_service_registered(connection, 0, psm);
12409d9bbc01Smatthias.ringwald }
12419d9bbc01Smatthias.ringwald 
124236944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){
1243e0abb8e7S[email protected] 
1244e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1245e0abb8e7S[email protected] 
12469d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1247037d6e48Smatthias.ringwald     if (!service) return;
12489d9bbc01Smatthias.ringwald     linked_list_remove(&l2cap_services, (linked_item_t *) service);
1249d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1250c0e866bfSmatthias.ringwald 
1251c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1252c0e866bfSmatthias.ringwald     if (!linked_list_empty(&l2cap_services)) return;
1253c0e866bfSmatthias.ringwald     hci_connectable_control(0);
12549d9bbc01Smatthias.ringwald }
12559d9bbc01Smatthias.ringwald 
12569d9bbc01Smatthias.ringwald //
125736944dffSmatthias.ringwald void l2cap_close_connection(void *connection){
12589d9bbc01Smatthias.ringwald     linked_item_t *it;
12599d9bbc01Smatthias.ringwald 
12603a9e0a58Smatthias.ringwald     // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
1261c52bf64dSmatthias.ringwald     l2cap_channel_t * channel;
1262c52bf64dSmatthias.ringwald     for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
1263c52bf64dSmatthias.ringwald         channel = (l2cap_channel_t *) it;
1264c52bf64dSmatthias.ringwald         if (channel->connection == connection) {
1265cb8eb7e6Smatthias.ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1266c52bf64dSmatthias.ringwald         }
1267c52bf64dSmatthias.ringwald     }
12689d9bbc01Smatthias.ringwald 
1269645658c9Smatthias.ringwald     // unregister services
127069025de8Smatthias.ringwald     it = (linked_item_t *) &l2cap_services;
127169025de8Smatthias.ringwald     while (it->next) {
127269025de8Smatthias.ringwald         l2cap_service_t * service = (l2cap_service_t *) it->next;
1273645658c9Smatthias.ringwald         if (service->connection == connection){
127469025de8Smatthias.ringwald             it->next = it->next->next;
1275d3a9df87Smatthias.ringwald             btstack_memory_l2cap_service_free(service);
12768149d2c7Smatthias.ringwald         } else {
12778149d2c7Smatthias.ringwald             it = it->next;
1278645658c9Smatthias.ringwald         }
1279645658c9Smatthias.ringwald     }
1280cb8eb7e6Smatthias.ringwald 
1281cb8eb7e6Smatthias.ringwald     // process
1282cb8eb7e6Smatthias.ringwald     l2cap_run();
1283c52bf64dSmatthias.ringwald }
12845652b5ffS[email protected] 
12855652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
12865652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
12875652b5ffS[email protected]     switch(channel_id){
12885652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
12895652b5ffS[email protected]             attribute_protocol_packet_handler = packet_handler;
12905652b5ffS[email protected]             break;
12915652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
12925652b5ffS[email protected]             security_protocol_packet_handler = packet_handler;
12935652b5ffS[email protected]             break;
12945652b5ffS[email protected]     }
12955652b5ffS[email protected] }
12965652b5ffS[email protected] 
1297ab2b01dcS[email protected] #ifdef HAVE_BLE
1298ab2b01dcS[email protected] // Request LE connection parameter update
1299ab2b01dcS[email protected] int l2cap_le_request_connection_parameter_update(uint16_t handle, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier){
1300ab2b01dcS[email protected]     if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
1301ab2b01dcS[email protected]         log_info("l2cap_send_signaling_packet, cannot send\n");
1302ab2b01dcS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
1303ab2b01dcS[email protected]     }
1304ab2b01dcS[email protected]     // log_info("l2cap_send_signaling_packet type %u\n", cmd);
1305ab2b01dcS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
1306ab2b01dcS[email protected]     uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier);
1307ab2b01dcS[email protected]     return hci_send_acl_packet(acl_buffer, len);
1308ab2b01dcS[email protected] }
1309ab2b01dcS[email protected] #endif
1310ab2b01dcS[email protected] 
1311