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 75c42e2ff2S[email protected] static linked_list_t l2cap_channels; 76c42e2ff2S[email protected] static linked_list_t l2cap_services; 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 80c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler; 81c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler; 82ac301f95S[email protected] static uint8_t require_security_level2_for_outgoing_sdp; 835652b5ffS[email protected] 8439bda6d5Smatthias.ringwald // prototypes 85fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 86fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm); 87fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 88fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 89fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 90fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 9139bda6d5Smatthias.ringwald 9239bda6d5Smatthias.ringwald 931e6aba47Smatthias.ringwald void l2cap_init(){ 94808a48abSmatthias.ringwald new_credits_blocked = 0; 952b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 96808a48abSmatthias.ringwald 97f5454fc6Smatthias.ringwald l2cap_channels = NULL; 98f5454fc6Smatthias.ringwald l2cap_services = NULL; 99f5454fc6Smatthias.ringwald 100f5454fc6Smatthias.ringwald packet_handler = null_packet_handler; 101c42e2ff2S[email protected] attribute_protocol_packet_handler = NULL; 102c42e2ff2S[email protected] security_protocol_packet_handler = NULL; 103f5454fc6Smatthias.ringwald 104ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 105ac301f95S[email protected] 106fcadd0caSmatthias.ringwald // 1072718e2e7Smatthias.ringwald // register callback with HCI 108fcadd0caSmatthias.ringwald // 1092718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 110c0e866bfSmatthias.ringwald hci_connectable_control(0); // no services yet 111fcadd0caSmatthias.ringwald } 112fcadd0caSmatthias.ringwald 113fcadd0caSmatthias.ringwald 114fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 11536944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 116fcadd0caSmatthias.ringwald } 11736944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 118b502e1b0Smatthias.ringwald packet_handler = handler; 1191e6aba47Smatthias.ringwald } 1201e6aba47Smatthias.ringwald 12158de5610Smatthias.ringwald // notify client/protocol handler 12258de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 12358de5610Smatthias.ringwald if (channel->packet_handler) { 12458de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 12558de5610Smatthias.ringwald } else { 12636944dffSmatthias.ringwald (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 12758de5610Smatthias.ringwald } 12858de5610Smatthias.ringwald } 12958de5610Smatthias.ringwald 13058de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 131c9dc710bS[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", 132e0abb8e7S[email protected] status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 133c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 134c9dc710bS[email protected] uint8_t event[23]; 13558de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 13658de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 13758de5610Smatthias.ringwald event[2] = status; 13858de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 13958de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 14058de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 14158de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 14258de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 1434c98aa43Smatthias.ringwald bt_store_16(event, 17, channel->local_mtu); 1444c98aa43Smatthias.ringwald bt_store_16(event, 19, channel->remote_mtu); 145c9dc710bS[email protected] bt_store_16(event, 19, channel->flush_timeout); 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_channel_closed(l2cap_channel_t *channel) { 151e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 15258de5610Smatthias.ringwald uint8_t event[4]; 15358de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 15458de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 15558de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 15658de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 15758de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 15858de5610Smatthias.ringwald } 15958de5610Smatthias.ringwald 16058de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 161e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 162e0abb8e7S[email protected] bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 16358de5610Smatthias.ringwald uint8_t event[16]; 16458de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 16558de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 16658de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 16758de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 16858de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 16958de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 17058de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 17158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17258de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1730af41d30Smatthias.ringwald } 174808a48abSmatthias.ringwald 1755842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ 176e0abb8e7S[email protected] log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); 1775842b6d9Smatthias.ringwald uint8_t event[5]; 1785842b6d9Smatthias.ringwald event[0] = L2CAP_EVENT_SERVICE_REGISTERED; 1795842b6d9Smatthias.ringwald event[1] = sizeof(event) - 2; 1805842b6d9Smatthias.ringwald event[2] = status; 1815842b6d9Smatthias.ringwald bt_store_16(event, 3, psm); 1825842b6d9Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1835842b6d9Smatthias.ringwald (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1845842b6d9Smatthias.ringwald } 1855842b6d9Smatthias.ringwald 1866218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 187e0abb8e7S[email protected] 188e0abb8e7S[email protected] log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits); 1896218e6f1Smatthias.ringwald // track credits 1906218e6f1Smatthias.ringwald channel->packets_granted += credits; 1916218e6f1Smatthias.ringwald 1926218e6f1Smatthias.ringwald uint8_t event[5]; 1936218e6f1Smatthias.ringwald event[0] = L2CAP_EVENT_CREDITS; 1946218e6f1Smatthias.ringwald event[1] = sizeof(event) - 2; 1956218e6f1Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 1966218e6f1Smatthias.ringwald event[4] = credits; 1976218e6f1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1986218e6f1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1996218e6f1Smatthias.ringwald } 2006218e6f1Smatthias.ringwald 201808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){ 202808a48abSmatthias.ringwald new_credits_blocked = blocked; 203808a48abSmatthias.ringwald } 204808a48abSmatthias.ringwald 20540d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){ 206808a48abSmatthias.ringwald 207808a48abSmatthias.ringwald if (new_credits_blocked) return; // we're told not to. used by daemon 208808a48abSmatthias.ringwald 2098d371091Smatthias.ringwald linked_item_t *it; 2108d371091Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 2118d371091Smatthias.ringwald if (!hci_number_free_acl_slots()) return; 2128d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 2130e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 2144c744e21Smatthias.ringwald if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 2158d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 2168d371091Smatthias.ringwald } 2178d371091Smatthias.ringwald } 2188d371091Smatthias.ringwald } 2198d371091Smatthias.ringwald 220b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 221f62db1e3Smatthias.ringwald linked_item_t *it; 222f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 2238d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 224b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 225f62db1e3Smatthias.ringwald return channel; 226f62db1e3Smatthias.ringwald } 227f62db1e3Smatthias.ringwald } 228f62db1e3Smatthias.ringwald return NULL; 229f62db1e3Smatthias.ringwald } 230f62db1e3Smatthias.ringwald 2316b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2326b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2336b1fde37Smatthias.ringwald if (!channel) return 0; 2346b1fde37Smatthias.ringwald if (!channel->packets_granted) return 0; 2357856fb31S[email protected] return hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET); 2367856fb31S[email protected] } 2377856fb31S[email protected] 2383cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){ 2393cab4fcaS[email protected] return hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET); 2403cab4fcaS[email protected] } 2413cab4fcaS[email protected] 24296cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 24396cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 24496cbd662Smatthias.ringwald if (channel) { 24596cbd662Smatthias.ringwald return channel->remote_mtu; 24696cbd662Smatthias.ringwald } 24796cbd662Smatthias.ringwald return 0; 24896cbd662Smatthias.ringwald } 24996cbd662Smatthias.ringwald 2505932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 2515932bd7cS[email protected] linked_item_t *it; 2525932bd7cS[email protected] for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 2535932bd7cS[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) it; 2545932bd7cS[email protected] if ( &channel->rtx == ts) { 2555932bd7cS[email protected] return channel; 2565932bd7cS[email protected] } 2575932bd7cS[email protected] } 2585932bd7cS[email protected] return NULL; 2595932bd7cS[email protected] } 2605932bd7cS[email protected] 2615932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){ 2625932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2635932bd7cS[email protected] if (!ts) return; 2645932bd7cS[email protected] 2655932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2665932bd7cS[email protected] 2675932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2685932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2695932bd7cS[email protected] // notify client 2705932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2715932bd7cS[email protected] 2725932bd7cS[email protected] // discard channel 2735932bd7cS[email protected] linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 2745932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 2755932bd7cS[email protected] } 2765932bd7cS[email protected] 2775932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 2785932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 2795932bd7cS[email protected] run_loop_remove_timer(&channel->rtx); 2805932bd7cS[email protected] } 2815932bd7cS[email protected] 2825932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 2835932bd7cS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 2845932bd7cS[email protected] l2cap_stop_rtx(channel); 2855932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 2865932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 2875932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 2885932bd7cS[email protected] } 2895932bd7cS[email protected] 2905932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 2915932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 2925932bd7cS[email protected] l2cap_stop_rtx(channel); 2935932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 2945932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 2955932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 2965932bd7cS[email protected] } 2975932bd7cS[email protected] 298ac301f95S[email protected] void l2cap_require_security_level_2_for_outgoing_sdp(){ 299ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 300ac301f95S[email protected] } 301ac301f95S[email protected] 302df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 303ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 304df3354fcS[email protected] } 3055932bd7cS[email protected] 30658de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 307b1d43497Smatthias.ringwald 3082a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){ 309b1d43497Smatthias.ringwald log_info("l2cap_send_signaling_packet, cannot send\n"); 310b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 311b1d43497Smatthias.ringwald } 312b1d43497Smatthias.ringwald 3137b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet type %u\n", cmd); 3142a373862S[email protected] hci_reserve_packet_buffer(); 315facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 31658de5610Smatthias.ringwald va_list argptr; 31758de5610Smatthias.ringwald va_start(argptr, identifier); 31870efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 31958de5610Smatthias.ringwald va_end(argptr); 3207b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet con %u!\n", handle); 321816c0598Smatthias.ringwald return hci_send_acl_packet(acl_buffer, len); 32258de5610Smatthias.ringwald } 32358de5610Smatthias.ringwald 32470efece1S[email protected] #ifdef HAVE_BLE 32570efece1S[email protected] int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 32670efece1S[email protected] 3272a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){ 32870efece1S[email protected] log_info("l2cap_send_signaling_packet, cannot send\n"); 32970efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 33070efece1S[email protected] } 33170efece1S[email protected] 33270efece1S[email protected] // log_info("l2cap_send_signaling_packet type %u\n", cmd); 3332a373862S[email protected] hci_reserve_packet_buffer(); 334facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 33570efece1S[email protected] va_list argptr; 33670efece1S[email protected] va_start(argptr, identifier); 33770efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 33870efece1S[email protected] va_end(argptr); 33970efece1S[email protected] // log_info("l2cap_send_signaling_packet con %u!\n", handle); 34070efece1S[email protected] return hci_send_acl_packet(acl_buffer, len); 34170efece1S[email protected] } 34270efece1S[email protected] #endif 34370efece1S[email protected] 344b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 345facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 346b1d43497Smatthias.ringwald } 3476218e6f1Smatthias.ringwald 3486b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3496b4af23dS[email protected] return hci_reserve_packet_buffer(); 3506b4af23dS[email protected] } 3516b4af23dS[email protected] 35268a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 35368a0fcf7S[email protected] hci_release_packet_buffer(); 35468a0fcf7S[email protected] } 35568a0fcf7S[email protected] 35668a0fcf7S[email protected] 357b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 358b1d43497Smatthias.ringwald 359c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 360c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 361c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 362c8b9416aS[email protected] } 363c8b9416aS[email protected] 364b1d43497Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 365c8b9416aS[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send\n", local_cid); 366808a48abSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 3678ea03fa5Smatthias.ringwald } 3686218e6f1Smatthias.ringwald 36958de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 370b1d43497Smatthias.ringwald if (!channel) { 371c8b9416aS[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x\n", local_cid); 372b1d43497Smatthias.ringwald return -1; // TODO: define error 3736218e6f1Smatthias.ringwald } 3746218e6f1Smatthias.ringwald 375b1d43497Smatthias.ringwald if (channel->packets_granted == 0){ 376c8b9416aS[email protected] log_error("l2cap_send_prepared cid 0x%02x, no credits!\n", local_cid); 377b1d43497Smatthias.ringwald return -1; // TODO: define error 378b1d43497Smatthias.ringwald } 379b1d43497Smatthias.ringwald 380b1d43497Smatthias.ringwald --channel->packets_granted; 381b1d43497Smatthias.ringwald 382c8b9416aS[email protected] log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used, credits left %u;\n", 383b1d43497Smatthias.ringwald local_cid, channel->handle, channel->packets_granted); 384b1d43497Smatthias.ringwald 385facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 386b1d43497Smatthias.ringwald 38758de5610Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 38858de5610Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 38958de5610Smatthias.ringwald // 2 - ACL length 39058de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 39158de5610Smatthias.ringwald // 4 - L2CAP packet length 39258de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 39358de5610Smatthias.ringwald // 6 - L2CAP channel DEST 39458de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 39558de5610Smatthias.ringwald // send 396b1d43497Smatthias.ringwald int err = hci_send_acl_packet(acl_buffer, len+8); 39791b99603Smatthias.ringwald 39891b99603Smatthias.ringwald l2cap_hand_out_credits(); 39991b99603Smatthias.ringwald 4006218e6f1Smatthias.ringwald return err; 40158de5610Smatthias.ringwald } 40258de5610Smatthias.ringwald 4032149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 4042149f12eSmatthias.ringwald 405c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 406c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4072149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4082149f12eSmatthias.ringwald } 4092149f12eSmatthias.ringwald 410c8b9416aS[email protected] if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 411c8b9416aS[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send\n", handle, cid); 412c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 413c8b9416aS[email protected] } 414c8b9416aS[email protected] 415c8b9416aS[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x\n", handle, cid); 4162149f12eSmatthias.ringwald 417facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4182149f12eSmatthias.ringwald 4192149f12eSmatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 4202149f12eSmatthias.ringwald bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14)); 4212149f12eSmatthias.ringwald // 2 - ACL length 4222149f12eSmatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 4232149f12eSmatthias.ringwald // 4 - L2CAP packet length 4242149f12eSmatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 4252149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 4262149f12eSmatthias.ringwald bt_store_16(acl_buffer, 6, cid); 4272149f12eSmatthias.ringwald // send 4282149f12eSmatthias.ringwald int err = hci_send_acl_packet(acl_buffer, len+8); 4292149f12eSmatthias.ringwald 4302149f12eSmatthias.ringwald l2cap_hand_out_credits(); 4312149f12eSmatthias.ringwald 4322149f12eSmatthias.ringwald return err; 4332149f12eSmatthias.ringwald } 4342149f12eSmatthias.ringwald 435b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 436b1d43497Smatthias.ringwald 4372a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){ 438e0abb8e7S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid); 439b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 440b1d43497Smatthias.ringwald } 441b1d43497Smatthias.ringwald 4422a373862S[email protected] hci_reserve_packet_buffer(); 443facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 444b1d43497Smatthias.ringwald 445b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 446b1d43497Smatthias.ringwald 447b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 448b1d43497Smatthias.ringwald } 449b1d43497Smatthias.ringwald 4502149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 4512149f12eSmatthias.ringwald 4522a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){ 453e0abb8e7S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send\n", cid); 4542149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4552149f12eSmatthias.ringwald } 4562149f12eSmatthias.ringwald 4572a373862S[email protected] hci_reserve_packet_buffer(); 458facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4592149f12eSmatthias.ringwald 4602149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 4612149f12eSmatthias.ringwald 4622149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 4632149f12eSmatthias.ringwald } 4642149f12eSmatthias.ringwald 4650e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 4660e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 4670e37e417S[email protected] } 4680e37e417S[email protected] 46928ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 47028ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 47128ca2b46S[email protected] } 47228ca2b46S[email protected] 47328ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 47428ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 47528ca2b46S[email protected] } 47628ca2b46S[email protected] 47728ca2b46S[email protected] 478b1d43497Smatthias.ringwald 4798158c421Smatthias.ringwald // MARK: L2CAP_RUN 4802cd0be45Smatthias.ringwald // process outstanding signaling tasks 4812cd0be45Smatthias.ringwald void l2cap_run(void){ 4822b83fb7dSmatthias.ringwald 4832b83fb7dSmatthias.ringwald // check pending signaling responses 4842b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 4852b83fb7dSmatthias.ringwald 4862a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)) break; 4872b83fb7dSmatthias.ringwald 4882b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 4892b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 4902b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 49163a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 4922b83fb7dSmatthias.ringwald 4932b83fb7dSmatthias.ringwald switch (signaling_responses[0].code){ 4942b360848Smatthias.ringwald case CONNECTION_REQUEST: 4952b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 4962bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 4972bd8b7e7S[email protected] hci_disconnect_security_block(handle); 4982b360848Smatthias.ringwald break; 4992b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5002b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5012b83fb7dSmatthias.ringwald break; 5022b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5033b0484b3S[email protected] switch (infoType){ 5043b0484b3S[email protected] case 1: { // Connectionless MTU 5053b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5063b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5073b0484b3S[email protected] break; 5083b0484b3S[email protected] } 5093b0484b3S[email protected] case 2: { // Extended Features Supported 5103b0484b3S[email protected] // extended features request supported, only supporing fixed channel map 5113b0484b3S[email protected] uint32_t features = 0x80; 5123b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5133b0484b3S[email protected] break; 5143b0484b3S[email protected] } 5153b0484b3S[email protected] case 3: { // Fixed Channels Supported 5163b0484b3S[email protected] uint8_t map[8]; 5173b0484b3S[email protected] memset(map, 0, 8); 5183b0484b3S[email protected] map[0] = 0x01; // L2CAP Signaling Channel 5193b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5203b0484b3S[email protected] break; 5213b0484b3S[email protected] } 5223b0484b3S[email protected] default: 5232b83fb7dSmatthias.ringwald // all other types are not supported 5242b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5253b0484b3S[email protected] break; 5262b83fb7dSmatthias.ringwald } 5272b83fb7dSmatthias.ringwald break; 52863a7246aSmatthias.ringwald case COMMAND_REJECT: 5295ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 53070efece1S[email protected] #ifdef HAVE_BLE 53170efece1S[email protected] case COMMAND_REJECT_LE: 53270efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 53363a7246aSmatthias.ringwald break; 53470efece1S[email protected] #endif 5352b83fb7dSmatthias.ringwald default: 5362b83fb7dSmatthias.ringwald // should not happen 5372b83fb7dSmatthias.ringwald break; 5382b83fb7dSmatthias.ringwald } 5392b83fb7dSmatthias.ringwald 5402b83fb7dSmatthias.ringwald // remove first item 5412b83fb7dSmatthias.ringwald signaling_responses_pending--; 5422b83fb7dSmatthias.ringwald int i; 5432b83fb7dSmatthias.ringwald for (i=0; i < signaling_responses_pending; i++){ 54476115249S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 5452b83fb7dSmatthias.ringwald } 5462b83fb7dSmatthias.ringwald } 5472b83fb7dSmatthias.ringwald 548ae280e73Smatthias.ringwald uint8_t config_options[4]; 5492cd0be45Smatthias.ringwald linked_item_t *it; 550756102d3Smatthias.ringwald linked_item_t *next; 551756102d3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = next){ 552756102d3Smatthias.ringwald next = it->next; // cache next item as current item might get freed 5532cd0be45Smatthias.ringwald 5542a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) break; 5552a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)) break; 5562cd0be45Smatthias.ringwald 5572cd0be45Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 5586e6710ebSmatthias.ringwald 5597b5fbe1fSmatthias.ringwald // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 5606e6710ebSmatthias.ringwald 56156081214Smatthias.ringwald 5622cd0be45Smatthias.ringwald switch (channel->state){ 5632cd0be45Smatthias.ringwald 564df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 565ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 566a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 567ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 568a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 569ad671560S[email protected] } 570ad671560S[email protected] break; 571ad671560S[email protected] 57202b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 57364472d52Smatthias.ringwald // send connection request - set state first 57464472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 57502b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 5768f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 57702b22dc4Smatthias.ringwald break; 57802b22dc4Smatthias.ringwald 579e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 5801eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 581e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 582756102d3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list 583d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 584e7ff783cSmatthias.ringwald break; 585e7ff783cSmatthias.ringwald 586552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 587fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 58828ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 5892a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 590552d92a1Smatthias.ringwald break; 591552d92a1Smatthias.ringwald 5926fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 5936fdcc387Smatthias.ringwald // success, start l2cap handshake 594b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 5956fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 5962a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 5975932bd7cS[email protected] l2cap_start_rtx(channel); 5986fdcc387Smatthias.ringwald break; 5996fdcc387Smatthias.ringwald 600fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 60173cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 60263a7246aSmatthias.ringwald uint16_t flags = 0; 60328ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 60463a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 60563a7246aSmatthias.ringwald flags = 1; 60663a7246aSmatthias.ringwald } else { 60728ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 60863a7246aSmatthias.ringwald } 60963a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 61063a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 61163a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 61263a7246aSmatthias.ringwald config_options[0] = 1; // MTU 61363a7246aSmatthias.ringwald config_options[1] = 2; // len param 61463a7246aSmatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 61563a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 61663a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 61763a7246aSmatthias.ringwald } else { 61863a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 61963a7246aSmatthias.ringwald } 62063a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 621fa8473a4Smatthias.ringwald } 62273cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 62328ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 62428ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 625b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 626ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 627ae280e73Smatthias.ringwald config_options[1] = 2; // len param 628ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 629b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6305932bd7cS[email protected] l2cap_start_rtx(channel); 631fa8473a4Smatthias.ringwald } 632fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 633552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 634552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 635552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 636fa8473a4Smatthias.ringwald } 637552d92a1Smatthias.ringwald break; 638552d92a1Smatthias.ringwald 639e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 640b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6415932bd7cS[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 :) 642756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 643e7ff783cSmatthias.ringwald break; 644e7ff783cSmatthias.ringwald 645e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 646b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6472cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 6482a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 6492cd0be45Smatthias.ringwald break; 6502cd0be45Smatthias.ringwald default: 6512cd0be45Smatthias.ringwald break; 6522cd0be45Smatthias.ringwald } 6532cd0be45Smatthias.ringwald } 6542cd0be45Smatthias.ringwald } 6552cd0be45Smatthias.ringwald 6564aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 657816c0598Smatthias.ringwald return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE; 658fa8c92f6Smatthias.ringwald } 659fa8c92f6Smatthias.ringwald 6602df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 6612df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 6625533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 6632df5dadcS[email protected] // success, start l2cap handshake 6642df5dadcS[email protected] channel->handle = handle; 6652df5dadcS[email protected] channel->local_cid = l2cap_next_local_cid(); 6662df5dadcS[email protected] // check remote SSP feature first 6672df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 6682df5dadcS[email protected] } 6692df5dadcS[email protected] } 6702df5dadcS[email protected] 6712df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 6722df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 6732df5dadcS[email protected] 6742df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 675ac301f95S[email protected] log_info("l2cap received remote supported features, sec_level_0_allowed for psm %u = %u", channel->psm, l2cap_security_level_0_allowed_for_PSM(channel->psm)); 6762df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 6772df5dadcS[email protected] // request security level 2 6782df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 6791429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 6802df5dadcS[email protected] return; 6812df5dadcS[email protected] } 6822df5dadcS[email protected] // fine, go ahead 6832df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 6842df5dadcS[email protected] } 6852df5dadcS[email protected] 6861e6aba47Smatthias.ringwald // open outgoing L2CAP channel 68715470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, 68815470d27Smatthias.ringwald bd_addr_t address, uint16_t psm, uint16_t mtu){ 6891e6aba47Smatthias.ringwald 690e0abb8e7S[email protected] log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 691e0abb8e7S[email protected] 6921e6aba47Smatthias.ringwald // alloc structure 69328ca2b46S[email protected] l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get(); 6942b360848Smatthias.ringwald if (!chan) { 6952b360848Smatthias.ringwald // emit error event 6962b360848Smatthias.ringwald l2cap_channel_t dummy_channel; 6972b360848Smatthias.ringwald BD_ADDR_COPY(dummy_channel.address, address); 6982b360848Smatthias.ringwald dummy_channel.psm = psm; 6992b360848Smatthias.ringwald l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 7002b360848Smatthias.ringwald return; 7012b360848Smatthias.ringwald } 7021d279b20Smatthias.ringwald // limit local mtu to max acl packet length 7032985cb84Smatthias.ringwald if (mtu > l2cap_max_mtu()) { 7042985cb84Smatthias.ringwald mtu = l2cap_max_mtu(); 7059775e25bSmatthias.ringwald } 7069775e25bSmatthias.ringwald 7071e6aba47Smatthias.ringwald // fill in 7081e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 7091e6aba47Smatthias.ringwald chan->psm = psm; 7101e6aba47Smatthias.ringwald chan->handle = 0; 7111e6aba47Smatthias.ringwald chan->connection = connection; 7126b296a27Smatthias.ringwald chan->packet_handler = packet_handler; 7132784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 71415470d27Smatthias.ringwald chan->local_mtu = mtu; 7156218e6f1Smatthias.ringwald chan->packets_granted = 0; 7166218e6f1Smatthias.ringwald 7171e6aba47Smatthias.ringwald // set initial state 71802b22dc4Smatthias.ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 71973cf2b3dSmatthias.ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 720b1988dceSmatthias.ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 721b1988dceSmatthias.ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 7225533f01eS[email protected] chan->required_security_level = LEVEL_0; 7231e6aba47Smatthias.ringwald 7241e6aba47Smatthias.ringwald // add to connections list 7251e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 7261e6aba47Smatthias.ringwald 7272df5dadcS[email protected] // check if hci connection is already usable 72896a45072S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type((bd_addr_t*)address, BD_ADDR_TYPE_CLASSIC); 7292df5dadcS[email protected] if (conn){ 7305533f01eS[email protected] log_info("l2cap_create_channel_internal, hci connection already exists"); 7312df5dadcS[email protected] l2cap_handle_connection_complete(conn->con_handle, chan); 7322df5dadcS[email protected] // check ir remote supported fearures are already received 7332df5dadcS[email protected] if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 7342df5dadcS[email protected] l2cap_handle_remote_supported_features_received(chan); 7352df5dadcS[email protected] } 7362df5dadcS[email protected] } 7372df5dadcS[email protected] 73802b22dc4Smatthias.ringwald l2cap_run(); 73943625864Smatthias.ringwald } 74043625864Smatthias.ringwald 741b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 742e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 743b35f641cSmatthias.ringwald // find channel for local_cid 744b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 745f62db1e3Smatthias.ringwald if (channel) { 746e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 747f62db1e3Smatthias.ringwald } 7482cd0be45Smatthias.ringwald // process 7492cd0be45Smatthias.ringwald l2cap_run(); 75043625864Smatthias.ringwald } 7511e6aba47Smatthias.ringwald 752afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 75315ec09bbSmatthias.ringwald linked_item_t *it = (linked_item_t *) &l2cap_channels; 75415ec09bbSmatthias.ringwald while (it->next){ 75515ec09bbSmatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 756b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 75702b22dc4Smatthias.ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 758afde0c52Smatthias.ringwald // failure, forward error code 759afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 760afde0c52Smatthias.ringwald // discard channel 76115ec09bbSmatthias.ringwald it->next = it->next->next; 762d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 763afde0c52Smatthias.ringwald } 76415ec09bbSmatthias.ringwald } else { 76515ec09bbSmatthias.ringwald it = it->next; 766afde0c52Smatthias.ringwald } 767afde0c52Smatthias.ringwald } 768afde0c52Smatthias.ringwald } 769afde0c52Smatthias.ringwald 770afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 771afde0c52Smatthias.ringwald linked_item_t *it; 772afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 773afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 774afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 7752df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 776afde0c52Smatthias.ringwald } 777afde0c52Smatthias.ringwald } 7786fdcc387Smatthias.ringwald // process 7796fdcc387Smatthias.ringwald l2cap_run(); 780afde0c52Smatthias.ringwald } 781b448a0e7Smatthias.ringwald 782afde0c52Smatthias.ringwald void l2cap_event_handler(uint8_t *packet, uint16_t size){ 783afde0c52Smatthias.ringwald 784afde0c52Smatthias.ringwald bd_addr_t address; 785afde0c52Smatthias.ringwald hci_con_handle_t handle; 7866e6710ebSmatthias.ringwald l2cap_channel_t * channel; 78736944dffSmatthias.ringwald linked_item_t *it; 7882d00edd4Smatthias.ringwald int hci_con_used; 789afde0c52Smatthias.ringwald 790afde0c52Smatthias.ringwald switch(packet[0]){ 791afde0c52Smatthias.ringwald 792afde0c52Smatthias.ringwald // handle connection complete events 793afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 794afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 795afde0c52Smatthias.ringwald if (packet[2] == 0){ 796afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 797afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 798afde0c52Smatthias.ringwald } else { 799afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 800afde0c52Smatthias.ringwald } 801afde0c52Smatthias.ringwald break; 802afde0c52Smatthias.ringwald 803afde0c52Smatthias.ringwald // handle successful create connection cancel command 804afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 805afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 806afde0c52Smatthias.ringwald if (packet[5] == 0){ 807afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 808afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 809afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 81003cfbabcSmatthias.ringwald } 8111e6aba47Smatthias.ringwald } 81239d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 81339d59809Smatthias.ringwald break; 81439d59809Smatthias.ringwald 81539d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 81639d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 817afde0c52Smatthias.ringwald break; 81827a923d0Smatthias.ringwald 8191e6aba47Smatthias.ringwald // handle disconnection complete events 820afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 82127a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 822afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 82315ec09bbSmatthias.ringwald it = (linked_item_t *) &l2cap_channels; 82415ec09bbSmatthias.ringwald while (it->next){ 82539ce35a7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 82627a923d0Smatthias.ringwald if ( channel->handle == handle ){ 82715ec09bbSmatthias.ringwald // update prev item before free'ing next element - don't call l2cap_finalize_channel_close 8282060cf14Smatthias.ringwald it->next = it->next->next; 82915ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 830d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 83115ec09bbSmatthias.ringwald } else { 83215ec09bbSmatthias.ringwald it = it->next; 83327a923d0Smatthias.ringwald } 83427a923d0Smatthias.ringwald } 835afde0c52Smatthias.ringwald break; 836fcadd0caSmatthias.ringwald 8376218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 83802b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 8398d371091Smatthias.ringwald l2cap_hand_out_credits(); 8406218e6f1Smatthias.ringwald break; 8416218e6f1Smatthias.ringwald 842ee091cf1Smatthias.ringwald // HCI Connection Timeouts 843afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 84436944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 84580ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 8462d00edd4Smatthias.ringwald hci_con_used = 0; 847ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 848ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 849ee091cf1Smatthias.ringwald if (channel->handle == handle) { 8502d00edd4Smatthias.ringwald hci_con_used = 1; 851ee091cf1Smatthias.ringwald } 852ee091cf1Smatthias.ringwald } 8532d00edd4Smatthias.ringwald if (hci_con_used) break; 8542a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) break; 8559edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 856afde0c52Smatthias.ringwald break; 857ee091cf1Smatthias.ringwald 8586e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 8596e6710ebSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 8606e6710ebSmatthias.ringwald channel = (l2cap_channel_t *) it; 8616e6710ebSmatthias.ringwald if (channel->packet_handler) { 8626e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 8636e6710ebSmatthias.ringwald } 8646e6710ebSmatthias.ringwald } 8655652b5ffS[email protected] if (attribute_protocol_packet_handler) { 8665652b5ffS[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 8675652b5ffS[email protected] } 8685652b5ffS[email protected] if (security_protocol_packet_handler) { 869133efcfdSmatthias.ringwald (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 8705652b5ffS[email protected] } 8716e6710ebSmatthias.ringwald break; 8726e6710ebSmatthias.ringwald 873df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 874df3354fcS[email protected] handle = READ_BT_16(packet, 3); 875df3354fcS[email protected] for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 876df3354fcS[email protected] channel = (l2cap_channel_t *) it; 877df3354fcS[email protected] if (channel->handle != handle) continue; 8782df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 879df3354fcS[email protected] break; 880df3354fcS[email protected] } 881df3354fcS[email protected] 882a00031e2S[email protected] case GAP_SECURITY_LEVEL: 883a00031e2S[email protected] handle = READ_BT_16(packet, 2); 884bd63148eS[email protected] log_info("l2cap - security level update"); 885f85a9399S[email protected] for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 886f85a9399S[email protected] channel = (l2cap_channel_t *) it; 887f85a9399S[email protected] if (channel->handle != handle) continue; 8885533f01eS[email protected] 889bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 890bd63148eS[email protected] 8915533f01eS[email protected] gap_security_level_t actual_level = packet[4]; 8925533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 8935533f01eS[email protected] 894df3354fcS[email protected] switch (channel->state){ 895df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 8965533f01eS[email protected] if (actual_level >= required_level){ 897f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 898f85a9399S[email protected] l2cap_emit_connection_request(channel); 8991eb2563eS[email protected] } else { 9001eb2563eS[email protected] channel->reason = 0x03; // security block 9011eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 9021eb2563eS[email protected] } 903df3354fcS[email protected] break; 904df3354fcS[email protected] 905df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 9065533f01eS[email protected] if (actual_level >= required_level){ 907df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 908df3354fcS[email protected] } else { 909df3354fcS[email protected] // disconnnect, authentication not good enough 910df3354fcS[email protected] hci_disconnect_security_block(handle); 911df3354fcS[email protected] } 912df3354fcS[email protected] break; 913df3354fcS[email protected] 914df3354fcS[email protected] default: 915df3354fcS[email protected] break; 916df3354fcS[email protected] } 917f85a9399S[email protected] } 918f85a9399S[email protected] break; 919f85a9399S[email protected] 920afde0c52Smatthias.ringwald default: 921afde0c52Smatthias.ringwald break; 922afde0c52Smatthias.ringwald } 923afde0c52Smatthias.ringwald 924*e0707417S[email protected] // pass on: main packet handler, att and sm packet handlers 925b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 926*e0707417S[email protected] if (attribute_protocol_packet_handler){ 927*e0707417S[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 928*e0707417S[email protected] } 929*e0707417S[email protected] if (security_protocol_packet_handler) { 930*e0707417S[email protected] (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 931*e0707417S[email protected] } 932bd63148eS[email protected] 933bd63148eS[email protected] l2cap_run(); 9341e6aba47Smatthias.ringwald } 9351e6aba47Smatthias.ringwald 936afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 937b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 938e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 939e7ff783cSmatthias.ringwald l2cap_run(); 94084836b65Smatthias.ringwald } 94184836b65Smatthias.ringwald 9422b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 9434cf56b4aSmatthias.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." 9442b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 9452b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 9462b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 9472b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 9482b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 9492b360848Smatthias.ringwald signaling_responses_pending++; 9502b360848Smatthias.ringwald l2cap_run(); 9512b360848Smatthias.ringwald } 9522b360848Smatthias.ringwald } 9532b360848Smatthias.ringwald 954b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 955645658c9Smatthias.ringwald 956e0abb8e7S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x\n", handle, psm, source_cid); 957645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 958645658c9Smatthias.ringwald if (!service) { 959645658c9Smatthias.ringwald // 0x0002 PSM not supported 9602b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 961645658c9Smatthias.ringwald return; 962645658c9Smatthias.ringwald } 963645658c9Smatthias.ringwald 9645061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 965645658c9Smatthias.ringwald if (!hci_connection) { 9662b360848Smatthias.ringwald // 9677d67539fSmatthias.ringwald log_error("no hci_connection for handle %u\n", handle); 968645658c9Smatthias.ringwald return; 969645658c9Smatthias.ringwald } 9702bd8b7e7S[email protected] 9712bd8b7e7S[email protected] // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP 972b087afb5S[email protected] if ( hci_ssp_supported_on_both_sides(handle) 973b087afb5S[email protected] && gap_security_level(handle) == LEVEL_0 974b087afb5S[email protected] && !l2cap_security_level_0_allowed_for_PSM(psm)){ 9752bd8b7e7S[email protected] 9762bd8b7e7S[email protected] // 0x0003 Security Block 9772bd8b7e7S[email protected] l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003); 9782bd8b7e7S[email protected] return; 9792bd8b7e7S[email protected] } 9802bd8b7e7S[email protected] 9812bd8b7e7S[email protected] 982645658c9Smatthias.ringwald // alloc structure 9837b5fbe1fSmatthias.ringwald // log_info("l2cap_handle_connection_request register channel\n"); 98428ca2b46S[email protected] l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get(); 9852b360848Smatthias.ringwald if (!channel){ 9862b360848Smatthias.ringwald // 0x0004 No resources available 9872b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 9882b360848Smatthias.ringwald return; 9892b360848Smatthias.ringwald } 990645658c9Smatthias.ringwald 991645658c9Smatthias.ringwald // fill in 992169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 993169f8b28Smatthias.ringwald channel->psm = psm; 994169f8b28Smatthias.ringwald channel->handle = handle; 995169f8b28Smatthias.ringwald channel->connection = service->connection; 996f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 997b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 998b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 999fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 10000a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1001761b0451Smatthias.ringwald channel->packets_granted = 0; 1002b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1003df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1004645658c9Smatthias.ringwald 10051d279b20Smatthias.ringwald // limit local mtu to max acl packet length 10062985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 10072985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 10089775e25bSmatthias.ringwald } 10099775e25bSmatthias.ringwald 1010645658c9Smatthias.ringwald // set initial state 1011df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1012ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1013e405ae81Smatthias.ringwald 1014645658c9Smatthias.ringwald // add to connections list 1015169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 1016645658c9Smatthias.ringwald 1017f85a9399S[email protected] // assert security requirements 10181eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1019e405ae81Smatthias.ringwald } 1020645658c9Smatthias.ringwald 1021b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 1022e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1023b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1024e405ae81Smatthias.ringwald if (!channel) { 10257d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 1026e405ae81Smatthias.ringwald return; 1027e405ae81Smatthias.ringwald } 1028e405ae81Smatthias.ringwald 1029552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1030e405ae81Smatthias.ringwald 1031552d92a1Smatthias.ringwald // process 1032552d92a1Smatthias.ringwald l2cap_run(); 1033e405ae81Smatthias.ringwald } 1034645658c9Smatthias.ringwald 1035b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 1036e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1037b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1038e405ae81Smatthias.ringwald if (!channel) { 10397d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 1040e405ae81Smatthias.ringwald return; 1041e405ae81Smatthias.ringwald } 1042e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1043e7ff783cSmatthias.ringwald channel->reason = reason; 1044e7ff783cSmatthias.ringwald l2cap_run(); 1045645658c9Smatthias.ringwald } 1046645658c9Smatthias.ringwald 10472784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1048b1988dceSmatthias.ringwald 1049b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1050b1988dceSmatthias.ringwald 105163a7246aSmatthias.ringwald uint16_t flags = READ_BT_16(command, 6); 105263a7246aSmatthias.ringwald if (flags & 1) { 105363a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 105463a7246aSmatthias.ringwald } 105563a7246aSmatthias.ringwald 10562784b77dSmatthias.ringwald // accept the other's configuration options 10573de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 10583de7c0caSmatthias.ringwald uint16_t pos = 8; 10593de7c0caSmatthias.ringwald while (pos < end_pos){ 106063a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 106163a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 106263a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 106363a7246aSmatthias.ringwald pos++; 10641dc511deSmatthias.ringwald uint8_t length = command[pos++]; 10651dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 106663a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 10671dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 1068e0abb8e7S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 106963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 107063a7246aSmatthias.ringwald } 10710fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 10720fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 10730fe7a9d0S[email protected] channel->flush_timeout = READ_BT_16(command, pos); 10740fe7a9d0S[email protected] } 107563a7246aSmatthias.ringwald // check for unknown options 107663a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1077c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 107863a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 10791dc511deSmatthias.ringwald } 10801dc511deSmatthias.ringwald pos += length; 10811dc511deSmatthias.ringwald } 10822784b77dSmatthias.ringwald } 10832784b77dSmatthias.ringwald 1084fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 10857b5fbe1fSmatthias.ringwald // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var); 108673cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 108773cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1088fa8473a4Smatthias.ringwald return 1; 1089fa8473a4Smatthias.ringwald } 1090fa8473a4Smatthias.ringwald 1091fa8473a4Smatthias.ringwald 109200d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 10931e6aba47Smatthias.ringwald 109400d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 109500d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 109638e5900eSmatthias.ringwald uint16_t result = 0; 10971e6aba47Smatthias.ringwald 10985842b6d9Smatthias.ringwald log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state); 1099b35f641cSmatthias.ringwald 11009a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 11019a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 11029a011532Smatthias.ringwald switch (channel->state){ 1103fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 11049a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 11052b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 11069a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 11079a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 11089a011532Smatthias.ringwald break; 11099a011532Smatthias.ringwald 11109a011532Smatthias.ringwald default: 11119a011532Smatthias.ringwald // ignore in other states 11129a011532Smatthias.ringwald break; 11139a011532Smatthias.ringwald } 11149a011532Smatthias.ringwald return; 11159a011532Smatthias.ringwald } 11169a011532Smatthias.ringwald 111756081214Smatthias.ringwald // @STATEMACHINE(l2cap) 11181e6aba47Smatthias.ringwald switch (channel->state) { 11191e6aba47Smatthias.ringwald 11201e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 11211e6aba47Smatthias.ringwald switch (code){ 11221e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 11235932bd7cS[email protected] l2cap_stop_rtx(channel); 112400d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 112538e5900eSmatthias.ringwald switch (result) { 112638e5900eSmatthias.ringwald case 0: 1127169f8b28Smatthias.ringwald // successful connection 112800d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1129fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 113028ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 113138e5900eSmatthias.ringwald break; 113238e5900eSmatthias.ringwald case 1: 11335932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 11345932bd7cS[email protected] l2cap_start_ertx(channel); 113538e5900eSmatthias.ringwald break; 113638e5900eSmatthias.ringwald default: 1137eb920dbeSmatthias.ringwald // channel closed 1138eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1139f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 114038e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1141eb920dbeSmatthias.ringwald 1142eb920dbeSmatthias.ringwald // drop link key if security block 1143eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 1144eb920dbeSmatthias.ringwald hci_drop_link_key_for_bd_addr(&channel->address); 1145eb920dbeSmatthias.ringwald } 1146eb920dbeSmatthias.ringwald 1147eb920dbeSmatthias.ringwald // discard channel 1148eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1149d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 115038e5900eSmatthias.ringwald break; 11511e6aba47Smatthias.ringwald } 11521e6aba47Smatthias.ringwald break; 115338e5900eSmatthias.ringwald 115438e5900eSmatthias.ringwald default: 11551e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 115638e5900eSmatthias.ringwald break; 11571e6aba47Smatthias.ringwald } 11581e6aba47Smatthias.ringwald break; 11591e6aba47Smatthias.ringwald 1160fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1161fe9d8984S[email protected] result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1162ae280e73Smatthias.ringwald switch (code) { 1163ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 116428ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1165ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 116663a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 116763a7246aSmatthias.ringwald // only done if continuation not set 116863a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 116963a7246aSmatthias.ringwald } 1170ae280e73Smatthias.ringwald break; 11711e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 11725932bd7cS[email protected] l2cap_stop_rtx(channel); 11735932bd7cS[email protected] switch (result){ 11745932bd7cS[email protected] case 0: // success 11755932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 11765932bd7cS[email protected] break; 11775932bd7cS[email protected] case 4: // pending 11785932bd7cS[email protected] l2cap_start_ertx(channel); 11795932bd7cS[email protected] break; 11805932bd7cS[email protected] default: 1181fe9d8984S[email protected] // retry on negative result 1182fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1183fe9d8984S[email protected] break; 1184fe9d8984S[email protected] } 11855a67bd4aSmatthias.ringwald break; 11865a67bd4aSmatthias.ringwald default: 11875a67bd4aSmatthias.ringwald break; 11881e6aba47Smatthias.ringwald } 1189fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1190fa8473a4Smatthias.ringwald // for open: 11915a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1192fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 11936218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 1194c8e4258aSmatthias.ringwald } 1195c8e4258aSmatthias.ringwald break; 1196f62db1e3Smatthias.ringwald 1197f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1198f62db1e3Smatthias.ringwald switch (code) { 1199f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 120027a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 120127a923d0Smatthias.ringwald break; 12025a67bd4aSmatthias.ringwald default: 12035a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 12045a67bd4aSmatthias.ringwald break; 120527a923d0Smatthias.ringwald } 120627a923d0Smatthias.ringwald break; 120784836b65Smatthias.ringwald 120884836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 120984836b65Smatthias.ringwald // @TODO handle incoming requests 121084836b65Smatthias.ringwald break; 121184836b65Smatthias.ringwald 121284836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 121384836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 121484836b65Smatthias.ringwald break; 121510642e45Smatthias.ringwald default: 121610642e45Smatthias.ringwald break; 121727a923d0Smatthias.ringwald } 12187b5fbe1fSmatthias.ringwald // log_info("new state %u\n", channel->state); 121927a923d0Smatthias.ringwald } 122027a923d0Smatthias.ringwald 122100d93d79Smatthias.ringwald 122200d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 122300d93d79Smatthias.ringwald 122400d93d79Smatthias.ringwald // get code, signalind identifier and command len 122500d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 122600d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 122700d93d79Smatthias.ringwald 122800d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 122900d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 123063a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 123100d93d79Smatthias.ringwald return; 123200d93d79Smatthias.ringwald } 123300d93d79Smatthias.ringwald 123400d93d79Smatthias.ringwald // general commands without an assigned channel 123500d93d79Smatthias.ringwald switch(code) { 123600d93d79Smatthias.ringwald 123700d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 123800d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 123900d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 124000d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 12412b83fb7dSmatthias.ringwald return; 124200d93d79Smatthias.ringwald } 124300d93d79Smatthias.ringwald 12442b360848Smatthias.ringwald case ECHO_REQUEST: 12452b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 12462b83fb7dSmatthias.ringwald return; 124700d93d79Smatthias.ringwald 124800d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 124900d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 12502b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 12512b83fb7dSmatthias.ringwald return; 125200d93d79Smatthias.ringwald } 125300d93d79Smatthias.ringwald 125400d93d79Smatthias.ringwald default: 125500d93d79Smatthias.ringwald break; 125600d93d79Smatthias.ringwald } 125700d93d79Smatthias.ringwald 125800d93d79Smatthias.ringwald 125900d93d79Smatthias.ringwald // Get potential destination CID 126000d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 126100d93d79Smatthias.ringwald 126200d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 126300d93d79Smatthias.ringwald linked_item_t *it; 126400d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 126500d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 126600d93d79Smatthias.ringwald if (channel->handle == handle) { 126700d93d79Smatthias.ringwald if (code & 1) { 1268b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1269b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 127000d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 12714e32727eSmatthias.ringwald break; 127200d93d79Smatthias.ringwald } 127300d93d79Smatthias.ringwald } else { 1274b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 127500d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 127600d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 12774e32727eSmatthias.ringwald break; 127800d93d79Smatthias.ringwald } 127900d93d79Smatthias.ringwald } 128000d93d79Smatthias.ringwald } 128100d93d79Smatthias.ringwald } 128200d93d79Smatthias.ringwald } 128300d93d79Smatthias.ringwald 128400d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 128500d93d79Smatthias.ringwald 128600d93d79Smatthias.ringwald // Get Channel ID 128700d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 128800d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 128900d93d79Smatthias.ringwald 12905652b5ffS[email protected] switch (channel_id) { 12915652b5ffS[email protected] 12925652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 12935652b5ffS[email protected] 129400d93d79Smatthias.ringwald uint16_t command_offset = 8; 129500d93d79Smatthias.ringwald while (command_offset < size) { 129600d93d79Smatthias.ringwald 129700d93d79Smatthias.ringwald // handle signaling commands 129800d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 129900d93d79Smatthias.ringwald 130000d93d79Smatthias.ringwald // increment command_offset 130100d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 130200d93d79Smatthias.ringwald } 13035652b5ffS[email protected] break; 130400d93d79Smatthias.ringwald } 130500d93d79Smatthias.ringwald 13065652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 13075652b5ffS[email protected] if (attribute_protocol_packet_handler) { 13085652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13095652b5ffS[email protected] } 13105652b5ffS[email protected] break; 13115652b5ffS[email protected] 13125652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 13135652b5ffS[email protected] if (security_protocol_packet_handler) { 13145652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13155652b5ffS[email protected] } 13165652b5ffS[email protected] break; 13175652b5ffS[email protected] 13181bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 13191bbc0b23S[email protected] // not implemented yet for LE Peripheral 13201bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 13211bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 13221bbc0b23S[email protected] break; 13231bbc0b23S[email protected] } 13241bbc0b23S[email protected] 13255652b5ffS[email protected] default: { 132600d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 132700d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 132800d93d79Smatthias.ringwald if (channel) { 132958de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 133000d93d79Smatthias.ringwald } 13315652b5ffS[email protected] break; 13325652b5ffS[email protected] } 13335652b5ffS[email protected] } 133400d93d79Smatthias.ringwald } 133500d93d79Smatthias.ringwald 13362718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 13372718e2e7Smatthias.ringwald switch (packet_type) { 13382718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 13392718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 13402718e2e7Smatthias.ringwald break; 13412718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 13422718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 13432718e2e7Smatthias.ringwald break; 13442718e2e7Smatthias.ringwald default: 13452718e2e7Smatthias.ringwald break; 13462718e2e7Smatthias.ringwald } 13471eb2563eS[email protected] l2cap_run(); 13482718e2e7Smatthias.ringwald } 134900d93d79Smatthias.ringwald 135015ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 135127a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1352f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1353f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1354f62db1e3Smatthias.ringwald // discard channel 1355f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1356d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1357c8e4258aSmatthias.ringwald } 13581e6aba47Smatthias.ringwald 13599d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 1360c52bf64dSmatthias.ringwald linked_item_t *it; 13619d9bbc01Smatthias.ringwald 13629d9bbc01Smatthias.ringwald // close open channels 13639d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 13649d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 13659d9bbc01Smatthias.ringwald if ( service->psm == psm){ 13669d9bbc01Smatthias.ringwald return service; 13679d9bbc01Smatthias.ringwald }; 13689d9bbc01Smatthias.ringwald } 13699d9bbc01Smatthias.ringwald return NULL; 13709d9bbc01Smatthias.ringwald } 13719d9bbc01Smatthias.ringwald 137262f901dfS[email protected] void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1373e0abb8e7S[email protected] 1374e0abb8e7S[email protected] log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1375e0abb8e7S[email protected] 13764bb582b6Smatthias.ringwald // check for alread registered psm 13774bb582b6Smatthias.ringwald // TODO: emit error event 13789d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1379277abc2cSmatthias.ringwald if (service) { 1380277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: PSM %u already registered\n", psm); 138181476041Smatthias.ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 1382277abc2cSmatthias.ringwald return; 1383277abc2cSmatthias.ringwald } 13849d9bbc01Smatthias.ringwald 13854bb582b6Smatthias.ringwald // alloc structure 13864bb582b6Smatthias.ringwald // TODO: emit error event 138728ca2b46S[email protected] service = (l2cap_service_t *) btstack_memory_l2cap_service_get(); 1388277abc2cSmatthias.ringwald if (!service) { 1389277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n"); 13905842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 1391277abc2cSmatthias.ringwald return; 1392277abc2cSmatthias.ringwald } 13939d9bbc01Smatthias.ringwald 13949d9bbc01Smatthias.ringwald // fill in 13959d9bbc01Smatthias.ringwald service->psm = psm; 13969d9bbc01Smatthias.ringwald service->mtu = mtu; 13979d9bbc01Smatthias.ringwald service->connection = connection; 1398d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 1399df3354fcS[email protected] service->required_security_level = security_level; 14009d9bbc01Smatthias.ringwald 14019d9bbc01Smatthias.ringwald // add to services list 14029d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 1403c0e866bfSmatthias.ringwald 1404c0e866bfSmatthias.ringwald // enable page scan 1405c0e866bfSmatthias.ringwald hci_connectable_control(1); 14065842b6d9Smatthias.ringwald 14075842b6d9Smatthias.ringwald // done 14085842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, 0, psm); 14099d9bbc01Smatthias.ringwald } 14109d9bbc01Smatthias.ringwald 141136944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 1412e0abb8e7S[email protected] 1413e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1414e0abb8e7S[email protected] 14159d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1416037d6e48Smatthias.ringwald if (!service) return; 14179d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 1418d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1419c0e866bfSmatthias.ringwald 1420c0e866bfSmatthias.ringwald // disable page scan when no services registered 1421c0e866bfSmatthias.ringwald if (!linked_list_empty(&l2cap_services)) return; 1422c0e866bfSmatthias.ringwald hci_connectable_control(0); 14239d9bbc01Smatthias.ringwald } 14249d9bbc01Smatthias.ringwald 14259d9bbc01Smatthias.ringwald // 142636944dffSmatthias.ringwald void l2cap_close_connection(void *connection){ 14279d9bbc01Smatthias.ringwald linked_item_t *it; 14289d9bbc01Smatthias.ringwald 14293a9e0a58Smatthias.ringwald // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks 1430c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 1431c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1432c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 1433c52bf64dSmatthias.ringwald if (channel->connection == connection) { 1434cb8eb7e6Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1435c52bf64dSmatthias.ringwald } 1436c52bf64dSmatthias.ringwald } 14379d9bbc01Smatthias.ringwald 1438645658c9Smatthias.ringwald // unregister services 143969025de8Smatthias.ringwald it = (linked_item_t *) &l2cap_services; 144069025de8Smatthias.ringwald while (it->next) { 144169025de8Smatthias.ringwald l2cap_service_t * service = (l2cap_service_t *) it->next; 1442645658c9Smatthias.ringwald if (service->connection == connection){ 144369025de8Smatthias.ringwald it->next = it->next->next; 1444d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 14458149d2c7Smatthias.ringwald } else { 14468149d2c7Smatthias.ringwald it = it->next; 1447645658c9Smatthias.ringwald } 1448645658c9Smatthias.ringwald } 1449cb8eb7e6Smatthias.ringwald 1450cb8eb7e6Smatthias.ringwald // process 1451cb8eb7e6Smatthias.ringwald l2cap_run(); 1452c52bf64dSmatthias.ringwald } 14535652b5ffS[email protected] 14545652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 14555652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 14565652b5ffS[email protected] switch(channel_id){ 14575652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 14585652b5ffS[email protected] attribute_protocol_packet_handler = packet_handler; 14595652b5ffS[email protected] break; 14605652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 14615652b5ffS[email protected] security_protocol_packet_handler = packet_handler; 14625652b5ffS[email protected] break; 14635652b5ffS[email protected] } 14645652b5ffS[email protected] } 14655652b5ffS[email protected] 1466ab2b01dcS[email protected] #ifdef HAVE_BLE 1467ab2b01dcS[email protected] // Request LE connection parameter update 1468ab2b01dcS[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){ 14692a373862S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_ACL_DATA_PACKET)){ 1470ab2b01dcS[email protected] log_info("l2cap_send_signaling_packet, cannot send\n"); 1471ab2b01dcS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 1472ab2b01dcS[email protected] } 1473ab2b01dcS[email protected] // log_info("l2cap_send_signaling_packet type %u\n", cmd); 1474facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 1475ab2b01dcS[email protected] uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier); 1476ab2b01dcS[email protected] return hci_send_acl_packet(acl_buffer, len); 1477ab2b01dcS[email protected] } 1478ab2b01dcS[email protected] #endif 1479ab2b01dcS[email protected] 1480