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; 825652b5ffS[email protected] 8339bda6d5Smatthias.ringwald // prototypes 84fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 85fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm); 86fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 87fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 88fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 89fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 9039bda6d5Smatthias.ringwald 9139bda6d5Smatthias.ringwald 921e6aba47Smatthias.ringwald void l2cap_init(){ 93808a48abSmatthias.ringwald new_credits_blocked = 0; 942b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 95808a48abSmatthias.ringwald 96f5454fc6Smatthias.ringwald l2cap_channels = NULL; 97f5454fc6Smatthias.ringwald l2cap_services = NULL; 98f5454fc6Smatthias.ringwald 99f5454fc6Smatthias.ringwald packet_handler = null_packet_handler; 100c42e2ff2S[email protected] attribute_protocol_packet_handler = NULL; 101c42e2ff2S[email protected] security_protocol_packet_handler = NULL; 102f5454fc6Smatthias.ringwald 103fcadd0caSmatthias.ringwald // 1042718e2e7Smatthias.ringwald // register callback with HCI 105fcadd0caSmatthias.ringwald // 1062718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 107c0e866bfSmatthias.ringwald hci_connectable_control(0); // no services yet 108fcadd0caSmatthias.ringwald } 109fcadd0caSmatthias.ringwald 110fcadd0caSmatthias.ringwald 111fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 11236944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 113fcadd0caSmatthias.ringwald } 11436944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 115b502e1b0Smatthias.ringwald packet_handler = handler; 1161e6aba47Smatthias.ringwald } 1171e6aba47Smatthias.ringwald 11858de5610Smatthias.ringwald // notify client/protocol handler 11958de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 12058de5610Smatthias.ringwald if (channel->packet_handler) { 12158de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 12258de5610Smatthias.ringwald } else { 12336944dffSmatthias.ringwald (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 12458de5610Smatthias.ringwald } 12558de5610Smatthias.ringwald } 12658de5610Smatthias.ringwald 12758de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 128c9dc710bS[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", 129e0abb8e7S[email protected] status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 130c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 131c9dc710bS[email protected] uint8_t event[23]; 13258de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 13358de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 13458de5610Smatthias.ringwald event[2] = status; 13558de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 13658de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 13758de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 13858de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 13958de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 1404c98aa43Smatthias.ringwald bt_store_16(event, 17, channel->local_mtu); 1414c98aa43Smatthias.ringwald bt_store_16(event, 19, channel->remote_mtu); 142c9dc710bS[email protected] bt_store_16(event, 19, channel->flush_timeout); 14358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 14458de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 14558de5610Smatthias.ringwald } 14658de5610Smatthias.ringwald 14758de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 148e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 14958de5610Smatthias.ringwald uint8_t event[4]; 15058de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 15158de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 15258de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 15358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 15458de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 15558de5610Smatthias.ringwald } 15658de5610Smatthias.ringwald 15758de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 158e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 159e0abb8e7S[email protected] bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 16058de5610Smatthias.ringwald uint8_t event[16]; 16158de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 16258de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 16358de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 16458de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 16558de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 16658de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 16758de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 16858de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 16958de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1700af41d30Smatthias.ringwald } 171808a48abSmatthias.ringwald 1725842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ 173e0abb8e7S[email protected] log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); 1745842b6d9Smatthias.ringwald uint8_t event[5]; 1755842b6d9Smatthias.ringwald event[0] = L2CAP_EVENT_SERVICE_REGISTERED; 1765842b6d9Smatthias.ringwald event[1] = sizeof(event) - 2; 1775842b6d9Smatthias.ringwald event[2] = status; 1785842b6d9Smatthias.ringwald bt_store_16(event, 3, psm); 1795842b6d9Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1805842b6d9Smatthias.ringwald (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1815842b6d9Smatthias.ringwald } 1825842b6d9Smatthias.ringwald 1836218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 184e0abb8e7S[email protected] 185e0abb8e7S[email protected] log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits); 1866218e6f1Smatthias.ringwald // track credits 1876218e6f1Smatthias.ringwald channel->packets_granted += credits; 1886218e6f1Smatthias.ringwald 1896218e6f1Smatthias.ringwald uint8_t event[5]; 1906218e6f1Smatthias.ringwald event[0] = L2CAP_EVENT_CREDITS; 1916218e6f1Smatthias.ringwald event[1] = sizeof(event) - 2; 1926218e6f1Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 1936218e6f1Smatthias.ringwald event[4] = credits; 1946218e6f1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1956218e6f1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1966218e6f1Smatthias.ringwald } 1976218e6f1Smatthias.ringwald 198808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){ 199808a48abSmatthias.ringwald new_credits_blocked = blocked; 200808a48abSmatthias.ringwald } 201808a48abSmatthias.ringwald 20240d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){ 203808a48abSmatthias.ringwald 204808a48abSmatthias.ringwald if (new_credits_blocked) return; // we're told not to. used by daemon 205808a48abSmatthias.ringwald 2068d371091Smatthias.ringwald linked_item_t *it; 2078d371091Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 2088d371091Smatthias.ringwald if (!hci_number_free_acl_slots()) return; 2098d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 2100e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 2114c744e21Smatthias.ringwald if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 2128d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 2138d371091Smatthias.ringwald } 2148d371091Smatthias.ringwald } 2158d371091Smatthias.ringwald } 2168d371091Smatthias.ringwald 217b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 218f62db1e3Smatthias.ringwald linked_item_t *it; 219f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 2208d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 221b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 222f62db1e3Smatthias.ringwald return channel; 223f62db1e3Smatthias.ringwald } 224f62db1e3Smatthias.ringwald } 225f62db1e3Smatthias.ringwald return NULL; 226f62db1e3Smatthias.ringwald } 227f62db1e3Smatthias.ringwald 2286b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2296b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2306b1fde37Smatthias.ringwald if (!channel) return 0; 2316b1fde37Smatthias.ringwald if (!channel->packets_granted) return 0; 2326b1fde37Smatthias.ringwald return hci_can_send_packet_now(HCI_ACL_DATA_PACKET); 2336b1fde37Smatthias.ringwald } 2346b1fde37Smatthias.ringwald 23596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 23696cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 23796cbd662Smatthias.ringwald if (channel) { 23896cbd662Smatthias.ringwald return channel->remote_mtu; 23996cbd662Smatthias.ringwald } 24096cbd662Smatthias.ringwald return 0; 24196cbd662Smatthias.ringwald } 24296cbd662Smatthias.ringwald 2435932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 2445932bd7cS[email protected] linked_item_t *it; 2455932bd7cS[email protected] for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 2465932bd7cS[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) it; 2475932bd7cS[email protected] if ( &channel->rtx == ts) { 2485932bd7cS[email protected] return channel; 2495932bd7cS[email protected] } 2505932bd7cS[email protected] } 2515932bd7cS[email protected] return NULL; 2525932bd7cS[email protected] } 2535932bd7cS[email protected] 2545932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){ 2555932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2565932bd7cS[email protected] if (!ts) return; 2575932bd7cS[email protected] 2585932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2595932bd7cS[email protected] 2605932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2615932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2625932bd7cS[email protected] // notify client 2635932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2645932bd7cS[email protected] 2655932bd7cS[email protected] // discard channel 2665932bd7cS[email protected] linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 2675932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 2685932bd7cS[email protected] } 2695932bd7cS[email protected] 2705932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 2715932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 2725932bd7cS[email protected] run_loop_remove_timer(&channel->rtx); 2735932bd7cS[email protected] } 2745932bd7cS[email protected] 2755932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 2765932bd7cS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 2775932bd7cS[email protected] l2cap_stop_rtx(channel); 2785932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 2795932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 2805932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 2815932bd7cS[email protected] } 2825932bd7cS[email protected] 2835932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 2845932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 2855932bd7cS[email protected] l2cap_stop_rtx(channel); 2865932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 2875932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 2885932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 2895932bd7cS[email protected] } 2905932bd7cS[email protected] 291df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 292df3354fcS[email protected] return psm != PSM_SDP; 293df3354fcS[email protected] } 2945932bd7cS[email protected] 29558de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 296b1d43497Smatthias.ringwald 297b1d43497Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 298b1d43497Smatthias.ringwald log_info("l2cap_send_signaling_packet, cannot send\n"); 299b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 300b1d43497Smatthias.ringwald } 301b1d43497Smatthias.ringwald 3027b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet type %u\n", cmd); 303b1d43497Smatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 30458de5610Smatthias.ringwald va_list argptr; 30558de5610Smatthias.ringwald va_start(argptr, identifier); 306816c0598Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr); 30758de5610Smatthias.ringwald va_end(argptr); 3087b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet con %u!\n", handle); 309816c0598Smatthias.ringwald return hci_send_acl_packet(acl_buffer, len); 31058de5610Smatthias.ringwald } 31158de5610Smatthias.ringwald 312b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 313b1d43497Smatthias.ringwald return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 314b1d43497Smatthias.ringwald } 3156218e6f1Smatthias.ringwald 316b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 317b1d43497Smatthias.ringwald 318b1d43497Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 319e0abb8e7S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid); 320808a48abSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 3218ea03fa5Smatthias.ringwald } 3226218e6f1Smatthias.ringwald 32358de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 324b1d43497Smatthias.ringwald if (!channel) { 325e0abb8e7S[email protected] log_error("l2cap_send_internal no channel for cid 0x%02x\n", local_cid); 326b1d43497Smatthias.ringwald return -1; // TODO: define error 3276218e6f1Smatthias.ringwald } 3286218e6f1Smatthias.ringwald 329b1d43497Smatthias.ringwald if (channel->packets_granted == 0){ 330e0abb8e7S[email protected] log_error("l2cap_send_internal cid 0x%02x, no credits!\n", local_cid); 331b1d43497Smatthias.ringwald return -1; // TODO: define error 332b1d43497Smatthias.ringwald } 333b1d43497Smatthias.ringwald 334b1d43497Smatthias.ringwald --channel->packets_granted; 335b1d43497Smatthias.ringwald 336e0abb8e7S[email protected] log_debug("l2cap_send_internal cid 0x%02x, handle %u, 1 credit used, credits left %u;\n", 337b1d43497Smatthias.ringwald local_cid, channel->handle, channel->packets_granted); 338b1d43497Smatthias.ringwald 339b1d43497Smatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 340b1d43497Smatthias.ringwald 34158de5610Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 34258de5610Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 34358de5610Smatthias.ringwald // 2 - ACL length 34458de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 34558de5610Smatthias.ringwald // 4 - L2CAP packet length 34658de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 34758de5610Smatthias.ringwald // 6 - L2CAP channel DEST 34858de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 34958de5610Smatthias.ringwald // send 350b1d43497Smatthias.ringwald int err = hci_send_acl_packet(acl_buffer, len+8); 35191b99603Smatthias.ringwald 35291b99603Smatthias.ringwald l2cap_hand_out_credits(); 35391b99603Smatthias.ringwald 3546218e6f1Smatthias.ringwald return err; 35558de5610Smatthias.ringwald } 35658de5610Smatthias.ringwald 3572149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 3582149f12eSmatthias.ringwald 3592149f12eSmatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 360e0abb8e7S[email protected] log_info("l2cap_send_prepared_to_handle cid 0x%02x, cannot send\n", cid); 3612149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 3622149f12eSmatthias.ringwald } 3632149f12eSmatthias.ringwald 364e0abb8e7S[email protected] log_debug("l2cap_send_prepared_to_handle cid 0x%02x, handle %u\n", cid, handle); 3652149f12eSmatthias.ringwald 3662149f12eSmatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 3672149f12eSmatthias.ringwald 3682149f12eSmatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 3692149f12eSmatthias.ringwald bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14)); 3702149f12eSmatthias.ringwald // 2 - ACL length 3712149f12eSmatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 3722149f12eSmatthias.ringwald // 4 - L2CAP packet length 3732149f12eSmatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 3742149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 3752149f12eSmatthias.ringwald bt_store_16(acl_buffer, 6, cid); 3762149f12eSmatthias.ringwald // send 3772149f12eSmatthias.ringwald int err = hci_send_acl_packet(acl_buffer, len+8); 3782149f12eSmatthias.ringwald 3792149f12eSmatthias.ringwald l2cap_hand_out_credits(); 3802149f12eSmatthias.ringwald 3812149f12eSmatthias.ringwald return err; 3822149f12eSmatthias.ringwald } 3832149f12eSmatthias.ringwald 384b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 385b1d43497Smatthias.ringwald 386b1d43497Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 387e0abb8e7S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid); 388b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 389b1d43497Smatthias.ringwald } 390b1d43497Smatthias.ringwald 391b1d43497Smatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 392b1d43497Smatthias.ringwald 393b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 394b1d43497Smatthias.ringwald 395b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 396b1d43497Smatthias.ringwald } 397b1d43497Smatthias.ringwald 3982149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 3992149f12eSmatthias.ringwald 4002149f12eSmatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 401e0abb8e7S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send\n", cid); 4022149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4032149f12eSmatthias.ringwald } 4042149f12eSmatthias.ringwald 4052149f12eSmatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 4062149f12eSmatthias.ringwald 4072149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 4082149f12eSmatthias.ringwald 4092149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 4102149f12eSmatthias.ringwald } 4112149f12eSmatthias.ringwald 4120e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 4130e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 4140e37e417S[email protected] } 4150e37e417S[email protected] 41628ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 41728ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 41828ca2b46S[email protected] } 41928ca2b46S[email protected] 42028ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 42128ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 42228ca2b46S[email protected] } 42328ca2b46S[email protected] 42428ca2b46S[email protected] 425b1d43497Smatthias.ringwald 4268158c421Smatthias.ringwald // MARK: L2CAP_RUN 4272cd0be45Smatthias.ringwald // process outstanding signaling tasks 4282cd0be45Smatthias.ringwald void l2cap_run(void){ 4292b83fb7dSmatthias.ringwald 4302b83fb7dSmatthias.ringwald // check pending signaling responses 4312b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 4322b83fb7dSmatthias.ringwald 43302b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 4342b83fb7dSmatthias.ringwald 4352b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 4362b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 4372b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 43863a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 4392b83fb7dSmatthias.ringwald 4402b83fb7dSmatthias.ringwald switch (signaling_responses[0].code){ 4412b360848Smatthias.ringwald case CONNECTION_REQUEST: 4422b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 4432bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 4442bd8b7e7S[email protected] hci_disconnect_security_block(handle); 4452b360848Smatthias.ringwald break; 4462b83fb7dSmatthias.ringwald case ECHO_REQUEST: 4472b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 4482b83fb7dSmatthias.ringwald break; 4492b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 4503b0484b3S[email protected] switch (infoType){ 4513b0484b3S[email protected] case 1: { // Connectionless MTU 4523b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 4533b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 4543b0484b3S[email protected] break; 4553b0484b3S[email protected] } 4563b0484b3S[email protected] case 2: { // Extended Features Supported 4573b0484b3S[email protected] // extended features request supported, only supporing fixed channel map 4583b0484b3S[email protected] uint32_t features = 0x80; 4593b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 4603b0484b3S[email protected] break; 4613b0484b3S[email protected] } 4623b0484b3S[email protected] case 3: { // Fixed Channels Supported 4633b0484b3S[email protected] uint8_t map[8]; 4643b0484b3S[email protected] memset(map, 0, 8); 4653b0484b3S[email protected] map[0] = 0x01; // L2CAP Signaling Channel 4663b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 4673b0484b3S[email protected] break; 4683b0484b3S[email protected] } 4693b0484b3S[email protected] default: 4702b83fb7dSmatthias.ringwald // all other types are not supported 4712b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 4723b0484b3S[email protected] break; 4732b83fb7dSmatthias.ringwald } 4742b83fb7dSmatthias.ringwald break; 47563a7246aSmatthias.ringwald case COMMAND_REJECT: 4765ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 47763a7246aSmatthias.ringwald break; 4782b83fb7dSmatthias.ringwald default: 4792b83fb7dSmatthias.ringwald // should not happen 4802b83fb7dSmatthias.ringwald break; 4812b83fb7dSmatthias.ringwald } 4822b83fb7dSmatthias.ringwald 4832b83fb7dSmatthias.ringwald // remove first item 4842b83fb7dSmatthias.ringwald signaling_responses_pending--; 4852b83fb7dSmatthias.ringwald int i; 4862b83fb7dSmatthias.ringwald for (i=0; i < signaling_responses_pending; i++){ 48776115249S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 4882b83fb7dSmatthias.ringwald } 4892b83fb7dSmatthias.ringwald } 4902b83fb7dSmatthias.ringwald 491ae280e73Smatthias.ringwald uint8_t config_options[4]; 4922cd0be45Smatthias.ringwald linked_item_t *it; 493756102d3Smatthias.ringwald linked_item_t *next; 494756102d3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = next){ 495756102d3Smatthias.ringwald next = it->next; // cache next item as current item might get freed 4962cd0be45Smatthias.ringwald 49702b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 49802b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 4992cd0be45Smatthias.ringwald 5002cd0be45Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 5016e6710ebSmatthias.ringwald 5027b5fbe1fSmatthias.ringwald // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 5036e6710ebSmatthias.ringwald 50456081214Smatthias.ringwald 5052cd0be45Smatthias.ringwald switch (channel->state){ 5062cd0be45Smatthias.ringwald 507df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 508ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 509a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 510ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 511a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 512ad671560S[email protected] } 513ad671560S[email protected] break; 514ad671560S[email protected] 51502b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 51664472d52Smatthias.ringwald // send connection request - set state first 51764472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 51802b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 5198f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 52002b22dc4Smatthias.ringwald break; 52102b22dc4Smatthias.ringwald 522e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 5231eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 524e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 525756102d3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list 526d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 527e7ff783cSmatthias.ringwald break; 528e7ff783cSmatthias.ringwald 529552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 530fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 53128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 5322a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 533552d92a1Smatthias.ringwald break; 534552d92a1Smatthias.ringwald 5356fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 5366fdcc387Smatthias.ringwald // success, start l2cap handshake 537b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 5386fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 5392a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 5405932bd7cS[email protected] l2cap_start_rtx(channel); 5416fdcc387Smatthias.ringwald break; 5426fdcc387Smatthias.ringwald 543fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 54473cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 54563a7246aSmatthias.ringwald uint16_t flags = 0; 54628ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 54763a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 54863a7246aSmatthias.ringwald flags = 1; 54963a7246aSmatthias.ringwald } else { 55028ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 55163a7246aSmatthias.ringwald } 55263a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 55363a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 55463a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 55563a7246aSmatthias.ringwald config_options[0] = 1; // MTU 55663a7246aSmatthias.ringwald config_options[1] = 2; // len param 55763a7246aSmatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 55863a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 55963a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 56063a7246aSmatthias.ringwald } else { 56163a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 56263a7246aSmatthias.ringwald } 56363a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 564fa8473a4Smatthias.ringwald } 56573cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 56628ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 56728ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 568b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 569ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 570ae280e73Smatthias.ringwald config_options[1] = 2; // len param 571ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 572b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 5735932bd7cS[email protected] l2cap_start_rtx(channel); 574fa8473a4Smatthias.ringwald } 575fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 576552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 577552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 578552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 579fa8473a4Smatthias.ringwald } 580552d92a1Smatthias.ringwald break; 581552d92a1Smatthias.ringwald 582e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 583b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 5845932bd7cS[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 :) 585756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 586e7ff783cSmatthias.ringwald break; 587e7ff783cSmatthias.ringwald 588e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 589b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 5902cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 5912a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 5922cd0be45Smatthias.ringwald break; 5932cd0be45Smatthias.ringwald default: 5942cd0be45Smatthias.ringwald break; 5952cd0be45Smatthias.ringwald } 5962cd0be45Smatthias.ringwald } 5972cd0be45Smatthias.ringwald } 5982cd0be45Smatthias.ringwald 5994aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 600816c0598Smatthias.ringwald return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE; 601fa8c92f6Smatthias.ringwald } 602fa8c92f6Smatthias.ringwald 6032df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 6042df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 605*5533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 6062df5dadcS[email protected] // success, start l2cap handshake 6072df5dadcS[email protected] channel->handle = handle; 6082df5dadcS[email protected] channel->local_cid = l2cap_next_local_cid(); 6092df5dadcS[email protected] // check remote SSP feature first 6102df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 6112df5dadcS[email protected] } 6122df5dadcS[email protected] } 6132df5dadcS[email protected] 6142df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 6152df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 6162df5dadcS[email protected] 6172df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 6182df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 6192df5dadcS[email protected] // request security level 2 6202df5dadcS[email protected] gap_request_security_level(channel->handle, LEVEL_2); 6212df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 6222df5dadcS[email protected] return; 6232df5dadcS[email protected] } 6242df5dadcS[email protected] // fine, go ahead 6252df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 6262df5dadcS[email protected] } 6272df5dadcS[email protected] 6281e6aba47Smatthias.ringwald // open outgoing L2CAP channel 62915470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, 63015470d27Smatthias.ringwald bd_addr_t address, uint16_t psm, uint16_t mtu){ 6311e6aba47Smatthias.ringwald 632e0abb8e7S[email protected] log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 633e0abb8e7S[email protected] 6341e6aba47Smatthias.ringwald // alloc structure 63528ca2b46S[email protected] l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get(); 6362b360848Smatthias.ringwald if (!chan) { 6372b360848Smatthias.ringwald // emit error event 6382b360848Smatthias.ringwald l2cap_channel_t dummy_channel; 6392b360848Smatthias.ringwald BD_ADDR_COPY(dummy_channel.address, address); 6402b360848Smatthias.ringwald dummy_channel.psm = psm; 6412b360848Smatthias.ringwald l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 6422b360848Smatthias.ringwald return; 6432b360848Smatthias.ringwald } 6441d279b20Smatthias.ringwald // limit local mtu to max acl packet length 6452985cb84Smatthias.ringwald if (mtu > l2cap_max_mtu()) { 6462985cb84Smatthias.ringwald mtu = l2cap_max_mtu(); 6479775e25bSmatthias.ringwald } 6489775e25bSmatthias.ringwald 6491e6aba47Smatthias.ringwald // fill in 6501e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 6511e6aba47Smatthias.ringwald chan->psm = psm; 6521e6aba47Smatthias.ringwald chan->handle = 0; 6531e6aba47Smatthias.ringwald chan->connection = connection; 6546b296a27Smatthias.ringwald chan->packet_handler = packet_handler; 6552784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 65615470d27Smatthias.ringwald chan->local_mtu = mtu; 6576218e6f1Smatthias.ringwald chan->packets_granted = 0; 6586218e6f1Smatthias.ringwald 6591e6aba47Smatthias.ringwald // set initial state 66002b22dc4Smatthias.ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 66173cf2b3dSmatthias.ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 662b1988dceSmatthias.ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 663b1988dceSmatthias.ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 664*5533f01eS[email protected] chan->required_security_level = LEVEL_0; 6651e6aba47Smatthias.ringwald 6661e6aba47Smatthias.ringwald // add to connections list 6671e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 6681e6aba47Smatthias.ringwald 6692df5dadcS[email protected] // check if hci connection is already usable 6702df5dadcS[email protected] hci_connection_t * conn = hci_connection_for_bd_addr((bd_addr_t*)address); 6712df5dadcS[email protected] if (conn){ 672*5533f01eS[email protected] log_info("l2cap_create_channel_internal, hci connection already exists"); 6732df5dadcS[email protected] l2cap_handle_connection_complete(conn->con_handle, chan); 6742df5dadcS[email protected] // check ir remote supported fearures are already received 6752df5dadcS[email protected] if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 6762df5dadcS[email protected] l2cap_handle_remote_supported_features_received(chan); 6772df5dadcS[email protected] } 6782df5dadcS[email protected] } 6792df5dadcS[email protected] 68002b22dc4Smatthias.ringwald l2cap_run(); 68143625864Smatthias.ringwald } 68243625864Smatthias.ringwald 683b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 684e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 685b35f641cSmatthias.ringwald // find channel for local_cid 686b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 687f62db1e3Smatthias.ringwald if (channel) { 688e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 689f62db1e3Smatthias.ringwald } 6902cd0be45Smatthias.ringwald // process 6912cd0be45Smatthias.ringwald l2cap_run(); 69243625864Smatthias.ringwald } 6931e6aba47Smatthias.ringwald 694afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 69515ec09bbSmatthias.ringwald linked_item_t *it = (linked_item_t *) &l2cap_channels; 69615ec09bbSmatthias.ringwald while (it->next){ 69715ec09bbSmatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 698b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 69902b22dc4Smatthias.ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 700afde0c52Smatthias.ringwald // failure, forward error code 701afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 702afde0c52Smatthias.ringwald // discard channel 70315ec09bbSmatthias.ringwald it->next = it->next->next; 704d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 705afde0c52Smatthias.ringwald } 70615ec09bbSmatthias.ringwald } else { 70715ec09bbSmatthias.ringwald it = it->next; 708afde0c52Smatthias.ringwald } 709afde0c52Smatthias.ringwald } 710afde0c52Smatthias.ringwald } 711afde0c52Smatthias.ringwald 712afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 713afde0c52Smatthias.ringwald linked_item_t *it; 714afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 715afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 716afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 7172df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 718afde0c52Smatthias.ringwald } 719afde0c52Smatthias.ringwald } 7206fdcc387Smatthias.ringwald // process 7216fdcc387Smatthias.ringwald l2cap_run(); 722afde0c52Smatthias.ringwald } 723b448a0e7Smatthias.ringwald 724afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 725afde0c52Smatthias.ringwald 726afde0c52Smatthias.ringwald bd_addr_t address; 727afde0c52Smatthias.ringwald hci_con_handle_t handle; 7286e6710ebSmatthias.ringwald l2cap_channel_t * channel; 72936944dffSmatthias.ringwald linked_item_t *it; 7302d00edd4Smatthias.ringwald int hci_con_used; 731afde0c52Smatthias.ringwald 732afde0c52Smatthias.ringwald switch(packet[0]){ 733afde0c52Smatthias.ringwald 734afde0c52Smatthias.ringwald // handle connection complete events 735afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 736afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 737afde0c52Smatthias.ringwald if (packet[2] == 0){ 738afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 739afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 740afde0c52Smatthias.ringwald } else { 741afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 742afde0c52Smatthias.ringwald } 743afde0c52Smatthias.ringwald break; 744afde0c52Smatthias.ringwald 745afde0c52Smatthias.ringwald // handle successful create connection cancel command 746afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 747afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 748afde0c52Smatthias.ringwald if (packet[5] == 0){ 749afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 750afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 751afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 75203cfbabcSmatthias.ringwald } 7531e6aba47Smatthias.ringwald } 75439d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 75539d59809Smatthias.ringwald break; 75639d59809Smatthias.ringwald 75739d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 75839d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 759afde0c52Smatthias.ringwald break; 76027a923d0Smatthias.ringwald 7611e6aba47Smatthias.ringwald // handle disconnection complete events 762afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 76327a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 764afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 76515ec09bbSmatthias.ringwald it = (linked_item_t *) &l2cap_channels; 76615ec09bbSmatthias.ringwald while (it->next){ 76739ce35a7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 76827a923d0Smatthias.ringwald if ( channel->handle == handle ){ 76915ec09bbSmatthias.ringwald // update prev item before free'ing next element - don't call l2cap_finalize_channel_close 7702060cf14Smatthias.ringwald it->next = it->next->next; 77115ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 772d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 77315ec09bbSmatthias.ringwald } else { 77415ec09bbSmatthias.ringwald it = it->next; 77527a923d0Smatthias.ringwald } 77627a923d0Smatthias.ringwald } 777afde0c52Smatthias.ringwald break; 778fcadd0caSmatthias.ringwald 7796218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 78002b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 7818d371091Smatthias.ringwald l2cap_hand_out_credits(); 7826218e6f1Smatthias.ringwald break; 7836218e6f1Smatthias.ringwald 784ee091cf1Smatthias.ringwald // HCI Connection Timeouts 785afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 78636944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 78780ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 7882d00edd4Smatthias.ringwald hci_con_used = 0; 789ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 790ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 791ee091cf1Smatthias.ringwald if (channel->handle == handle) { 7922d00edd4Smatthias.ringwald hci_con_used = 1; 793ee091cf1Smatthias.ringwald } 794ee091cf1Smatthias.ringwald } 7952d00edd4Smatthias.ringwald if (hci_con_used) break; 79632ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 7979edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 798afde0c52Smatthias.ringwald break; 799ee091cf1Smatthias.ringwald 8006e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 8016e6710ebSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 8026e6710ebSmatthias.ringwald channel = (l2cap_channel_t *) it; 8036e6710ebSmatthias.ringwald if (channel->packet_handler) { 8046e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 8056e6710ebSmatthias.ringwald } 8066e6710ebSmatthias.ringwald } 8075652b5ffS[email protected] if (attribute_protocol_packet_handler) { 8085652b5ffS[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 8095652b5ffS[email protected] } 8105652b5ffS[email protected] if (security_protocol_packet_handler) { 811133efcfdSmatthias.ringwald (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 8125652b5ffS[email protected] } 8136e6710ebSmatthias.ringwald break; 8146e6710ebSmatthias.ringwald 815df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 816df3354fcS[email protected] handle = READ_BT_16(packet, 3); 817df3354fcS[email protected] for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 818df3354fcS[email protected] channel = (l2cap_channel_t *) it; 819df3354fcS[email protected] if (channel->handle != handle) continue; 8202df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 821df3354fcS[email protected] break; 822df3354fcS[email protected] } 823df3354fcS[email protected] 824a00031e2S[email protected] case GAP_SECURITY_LEVEL: 825a00031e2S[email protected] handle = READ_BT_16(packet, 2); 826df3354fcS[email protected] log_info("GAP_SECURITY_LEVEL"); 827f85a9399S[email protected] for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 828f85a9399S[email protected] channel = (l2cap_channel_t *) it; 829f85a9399S[email protected] if (channel->handle != handle) continue; 830*5533f01eS[email protected] 831*5533f01eS[email protected] gap_security_level_t actual_level = packet[4]; 832*5533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 833*5533f01eS[email protected] log_info("gap outgoing - security level update %u, required %u", actual_level, required_level); 834*5533f01eS[email protected] 835df3354fcS[email protected] switch (channel->state){ 836df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 837df3354fcS[email protected] log_info("gap incoming"); 838*5533f01eS[email protected] if (actual_level >= required_level){ 839f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 840f85a9399S[email protected] l2cap_emit_connection_request(channel); 8411eb2563eS[email protected] } else { 8421eb2563eS[email protected] channel->reason = 0x03; // security block 8431eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 8441eb2563eS[email protected] } 845df3354fcS[email protected] break; 846df3354fcS[email protected] 847df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 848*5533f01eS[email protected] if (actual_level >= required_level){ 849df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 850df3354fcS[email protected] } else { 851df3354fcS[email protected] // disconnnect, authentication not good enough 852df3354fcS[email protected] hci_disconnect_security_block(handle); 853df3354fcS[email protected] } 854df3354fcS[email protected] break; 855df3354fcS[email protected] 856df3354fcS[email protected] default: 857df3354fcS[email protected] break; 858df3354fcS[email protected] } 859f85a9399S[email protected] } 860f85a9399S[email protected] break; 861f85a9399S[email protected] 862afde0c52Smatthias.ringwald default: 863afde0c52Smatthias.ringwald break; 864afde0c52Smatthias.ringwald } 865afde0c52Smatthias.ringwald 866afde0c52Smatthias.ringwald // pass on 867b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 8681e6aba47Smatthias.ringwald } 8691e6aba47Smatthias.ringwald 870afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 871b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 872e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 873e7ff783cSmatthias.ringwald l2cap_run(); 87484836b65Smatthias.ringwald } 87584836b65Smatthias.ringwald 8762b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 8774cf56b4aSmatthias.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." 8782b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 8792b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 8802b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 8812b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 8822b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 8832b360848Smatthias.ringwald signaling_responses_pending++; 8842b360848Smatthias.ringwald l2cap_run(); 8852b360848Smatthias.ringwald } 8862b360848Smatthias.ringwald } 8872b360848Smatthias.ringwald 888b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 889645658c9Smatthias.ringwald 890e0abb8e7S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x\n", handle, psm, source_cid); 891645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 892645658c9Smatthias.ringwald if (!service) { 893645658c9Smatthias.ringwald // 0x0002 PSM not supported 8942b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 895645658c9Smatthias.ringwald return; 896645658c9Smatthias.ringwald } 897645658c9Smatthias.ringwald 8985061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 899645658c9Smatthias.ringwald if (!hci_connection) { 9002b360848Smatthias.ringwald // 9017d67539fSmatthias.ringwald log_error("no hci_connection for handle %u\n", handle); 902645658c9Smatthias.ringwald return; 903645658c9Smatthias.ringwald } 9042bd8b7e7S[email protected] 9052bd8b7e7S[email protected] // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP 906df3354fcS[email protected] if ( l2cap_security_level_0_allowed_for_PSM(psm) 907df3354fcS[email protected] && hci_ssp_supported_on_both_sides(handle) 908df3354fcS[email protected] && gap_security_level(handle) == LEVEL_0){ 9092bd8b7e7S[email protected] 9102bd8b7e7S[email protected] // 0x0003 Security Block 9112bd8b7e7S[email protected] l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003); 9122bd8b7e7S[email protected] return; 9132bd8b7e7S[email protected] } 9142bd8b7e7S[email protected] 9152bd8b7e7S[email protected] 916645658c9Smatthias.ringwald // alloc structure 9177b5fbe1fSmatthias.ringwald // log_info("l2cap_handle_connection_request register channel\n"); 91828ca2b46S[email protected] l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get(); 9192b360848Smatthias.ringwald if (!channel){ 9202b360848Smatthias.ringwald // 0x0004 No resources available 9212b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 9222b360848Smatthias.ringwald return; 9232b360848Smatthias.ringwald } 924645658c9Smatthias.ringwald 925645658c9Smatthias.ringwald // fill in 926169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 927169f8b28Smatthias.ringwald channel->psm = psm; 928169f8b28Smatthias.ringwald channel->handle = handle; 929169f8b28Smatthias.ringwald channel->connection = service->connection; 930f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 931b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 932b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 933fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 9340a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 935761b0451Smatthias.ringwald channel->packets_granted = 0; 936b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 937df3354fcS[email protected] channel->required_security_level = service->required_security_level; 938645658c9Smatthias.ringwald 9391d279b20Smatthias.ringwald // limit local mtu to max acl packet length 9402985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 9412985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 9429775e25bSmatthias.ringwald } 9439775e25bSmatthias.ringwald 944645658c9Smatthias.ringwald // set initial state 945df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 946ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 947e405ae81Smatthias.ringwald 948645658c9Smatthias.ringwald // add to connections list 949169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 950645658c9Smatthias.ringwald 951f85a9399S[email protected] // assert security requirements 9521eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 953e405ae81Smatthias.ringwald } 954645658c9Smatthias.ringwald 955b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 956e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 957b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 958e405ae81Smatthias.ringwald if (!channel) { 9597d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 960e405ae81Smatthias.ringwald return; 961e405ae81Smatthias.ringwald } 962e405ae81Smatthias.ringwald 963552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 964e405ae81Smatthias.ringwald 965552d92a1Smatthias.ringwald // process 966552d92a1Smatthias.ringwald l2cap_run(); 967e405ae81Smatthias.ringwald } 968645658c9Smatthias.ringwald 969b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 970e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 971b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 972e405ae81Smatthias.ringwald if (!channel) { 9737d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 974e405ae81Smatthias.ringwald return; 975e405ae81Smatthias.ringwald } 976e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 977e7ff783cSmatthias.ringwald channel->reason = reason; 978e7ff783cSmatthias.ringwald l2cap_run(); 979645658c9Smatthias.ringwald } 980645658c9Smatthias.ringwald 9812784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 982b1988dceSmatthias.ringwald 983b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 984b1988dceSmatthias.ringwald 98563a7246aSmatthias.ringwald uint16_t flags = READ_BT_16(command, 6); 98663a7246aSmatthias.ringwald if (flags & 1) { 98763a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 98863a7246aSmatthias.ringwald } 98963a7246aSmatthias.ringwald 9902784b77dSmatthias.ringwald // accept the other's configuration options 9913de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 9923de7c0caSmatthias.ringwald uint16_t pos = 8; 9933de7c0caSmatthias.ringwald while (pos < end_pos){ 99463a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 99563a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 99663a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 99763a7246aSmatthias.ringwald pos++; 9981dc511deSmatthias.ringwald uint8_t length = command[pos++]; 9991dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 100063a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 10011dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 1002e0abb8e7S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 100363a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 100463a7246aSmatthias.ringwald } 10050fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 10060fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 10070fe7a9d0S[email protected] channel->flush_timeout = READ_BT_16(command, pos); 10080fe7a9d0S[email protected] } 100963a7246aSmatthias.ringwald // check for unknown options 101063a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1011c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 101263a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 10131dc511deSmatthias.ringwald } 10141dc511deSmatthias.ringwald pos += length; 10151dc511deSmatthias.ringwald } 10162784b77dSmatthias.ringwald } 10172784b77dSmatthias.ringwald 1018fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 10197b5fbe1fSmatthias.ringwald // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var); 102073cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 102173cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1022fa8473a4Smatthias.ringwald return 1; 1023fa8473a4Smatthias.ringwald } 1024fa8473a4Smatthias.ringwald 1025fa8473a4Smatthias.ringwald 102600d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 10271e6aba47Smatthias.ringwald 102800d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 102900d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 103038e5900eSmatthias.ringwald uint16_t result = 0; 10311e6aba47Smatthias.ringwald 10325842b6d9Smatthias.ringwald log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state); 1033b35f641cSmatthias.ringwald 10349a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 10359a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 10369a011532Smatthias.ringwald switch (channel->state){ 1037fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 10389a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 10392b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 10409a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 10419a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 10429a011532Smatthias.ringwald break; 10439a011532Smatthias.ringwald 10449a011532Smatthias.ringwald default: 10459a011532Smatthias.ringwald // ignore in other states 10469a011532Smatthias.ringwald break; 10479a011532Smatthias.ringwald } 10489a011532Smatthias.ringwald return; 10499a011532Smatthias.ringwald } 10509a011532Smatthias.ringwald 105156081214Smatthias.ringwald // @STATEMACHINE(l2cap) 10521e6aba47Smatthias.ringwald switch (channel->state) { 10531e6aba47Smatthias.ringwald 10541e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 10551e6aba47Smatthias.ringwald switch (code){ 10561e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 10575932bd7cS[email protected] l2cap_stop_rtx(channel); 105800d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 105938e5900eSmatthias.ringwald switch (result) { 106038e5900eSmatthias.ringwald case 0: 1061169f8b28Smatthias.ringwald // successful connection 106200d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1063fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 106428ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 106538e5900eSmatthias.ringwald break; 106638e5900eSmatthias.ringwald case 1: 10675932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 10685932bd7cS[email protected] l2cap_start_ertx(channel); 106938e5900eSmatthias.ringwald break; 107038e5900eSmatthias.ringwald default: 1071eb920dbeSmatthias.ringwald // channel closed 1072eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1073f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 107438e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1075eb920dbeSmatthias.ringwald 1076eb920dbeSmatthias.ringwald // drop link key if security block 1077eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 1078eb920dbeSmatthias.ringwald hci_drop_link_key_for_bd_addr(&channel->address); 1079eb920dbeSmatthias.ringwald } 1080eb920dbeSmatthias.ringwald 1081eb920dbeSmatthias.ringwald // discard channel 1082eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1083d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 108438e5900eSmatthias.ringwald break; 10851e6aba47Smatthias.ringwald } 10861e6aba47Smatthias.ringwald break; 108738e5900eSmatthias.ringwald 108838e5900eSmatthias.ringwald default: 10891e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 109038e5900eSmatthias.ringwald break; 10911e6aba47Smatthias.ringwald } 10921e6aba47Smatthias.ringwald break; 10931e6aba47Smatthias.ringwald 1094fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1095fe9d8984S[email protected] result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1096ae280e73Smatthias.ringwald switch (code) { 1097ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 109828ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1099ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 110063a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 110163a7246aSmatthias.ringwald // only done if continuation not set 110263a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 110363a7246aSmatthias.ringwald } 1104ae280e73Smatthias.ringwald break; 11051e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 11065932bd7cS[email protected] l2cap_stop_rtx(channel); 11075932bd7cS[email protected] switch (result){ 11085932bd7cS[email protected] case 0: // success 11095932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 11105932bd7cS[email protected] break; 11115932bd7cS[email protected] case 4: // pending 11125932bd7cS[email protected] l2cap_start_ertx(channel); 11135932bd7cS[email protected] break; 11145932bd7cS[email protected] default: 1115fe9d8984S[email protected] // retry on negative result 1116fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1117fe9d8984S[email protected] break; 1118fe9d8984S[email protected] } 11195a67bd4aSmatthias.ringwald break; 11205a67bd4aSmatthias.ringwald default: 11215a67bd4aSmatthias.ringwald break; 11221e6aba47Smatthias.ringwald } 1123fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1124fa8473a4Smatthias.ringwald // for open: 11255a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1126fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 11276218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 1128c8e4258aSmatthias.ringwald } 1129c8e4258aSmatthias.ringwald break; 1130f62db1e3Smatthias.ringwald 1131f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1132f62db1e3Smatthias.ringwald switch (code) { 1133f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 113427a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 113527a923d0Smatthias.ringwald break; 11365a67bd4aSmatthias.ringwald default: 11375a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 11385a67bd4aSmatthias.ringwald break; 113927a923d0Smatthias.ringwald } 114027a923d0Smatthias.ringwald break; 114184836b65Smatthias.ringwald 114284836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 114384836b65Smatthias.ringwald // @TODO handle incoming requests 114484836b65Smatthias.ringwald break; 114584836b65Smatthias.ringwald 114684836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 114784836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 114884836b65Smatthias.ringwald break; 114910642e45Smatthias.ringwald default: 115010642e45Smatthias.ringwald break; 115127a923d0Smatthias.ringwald } 11527b5fbe1fSmatthias.ringwald // log_info("new state %u\n", channel->state); 115327a923d0Smatthias.ringwald } 115427a923d0Smatthias.ringwald 115500d93d79Smatthias.ringwald 115600d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 115700d93d79Smatthias.ringwald 115800d93d79Smatthias.ringwald // get code, signalind identifier and command len 115900d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 116000d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 116100d93d79Smatthias.ringwald 116200d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 116300d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 116463a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 116500d93d79Smatthias.ringwald return; 116600d93d79Smatthias.ringwald } 116700d93d79Smatthias.ringwald 116800d93d79Smatthias.ringwald // general commands without an assigned channel 116900d93d79Smatthias.ringwald switch(code) { 117000d93d79Smatthias.ringwald 117100d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 117200d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 117300d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 117400d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 11752b83fb7dSmatthias.ringwald return; 117600d93d79Smatthias.ringwald } 117700d93d79Smatthias.ringwald 11782b360848Smatthias.ringwald case ECHO_REQUEST: 11792b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 11802b83fb7dSmatthias.ringwald return; 118100d93d79Smatthias.ringwald 118200d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 118300d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 11842b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 11852b83fb7dSmatthias.ringwald return; 118600d93d79Smatthias.ringwald } 118700d93d79Smatthias.ringwald 118800d93d79Smatthias.ringwald default: 118900d93d79Smatthias.ringwald break; 119000d93d79Smatthias.ringwald } 119100d93d79Smatthias.ringwald 119200d93d79Smatthias.ringwald 119300d93d79Smatthias.ringwald // Get potential destination CID 119400d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 119500d93d79Smatthias.ringwald 119600d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 119700d93d79Smatthias.ringwald linked_item_t *it; 119800d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 119900d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 120000d93d79Smatthias.ringwald if (channel->handle == handle) { 120100d93d79Smatthias.ringwald if (code & 1) { 1202b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1203b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 120400d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 12054e32727eSmatthias.ringwald break; 120600d93d79Smatthias.ringwald } 120700d93d79Smatthias.ringwald } else { 1208b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 120900d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 121000d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 12114e32727eSmatthias.ringwald break; 121200d93d79Smatthias.ringwald } 121300d93d79Smatthias.ringwald } 121400d93d79Smatthias.ringwald } 121500d93d79Smatthias.ringwald } 121600d93d79Smatthias.ringwald } 121700d93d79Smatthias.ringwald 121800d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 121900d93d79Smatthias.ringwald 122000d93d79Smatthias.ringwald // Get Channel ID 122100d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 122200d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 122300d93d79Smatthias.ringwald 12245652b5ffS[email protected] switch (channel_id) { 12255652b5ffS[email protected] 12265652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 12275652b5ffS[email protected] 122800d93d79Smatthias.ringwald uint16_t command_offset = 8; 122900d93d79Smatthias.ringwald while (command_offset < size) { 123000d93d79Smatthias.ringwald 123100d93d79Smatthias.ringwald // handle signaling commands 123200d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 123300d93d79Smatthias.ringwald 123400d93d79Smatthias.ringwald // increment command_offset 123500d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 123600d93d79Smatthias.ringwald } 12375652b5ffS[email protected] break; 123800d93d79Smatthias.ringwald } 123900d93d79Smatthias.ringwald 12405652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 12415652b5ffS[email protected] if (attribute_protocol_packet_handler) { 12425652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 12435652b5ffS[email protected] } 12445652b5ffS[email protected] break; 12455652b5ffS[email protected] 12465652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 12475652b5ffS[email protected] if (security_protocol_packet_handler) { 12485652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 12495652b5ffS[email protected] } 12505652b5ffS[email protected] break; 12515652b5ffS[email protected] 12525652b5ffS[email protected] default: { 125300d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 125400d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 125500d93d79Smatthias.ringwald if (channel) { 125658de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 125700d93d79Smatthias.ringwald } 12585652b5ffS[email protected] break; 12595652b5ffS[email protected] } 12605652b5ffS[email protected] } 126100d93d79Smatthias.ringwald } 126200d93d79Smatthias.ringwald 12632718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 12642718e2e7Smatthias.ringwald switch (packet_type) { 12652718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 12662718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 12672718e2e7Smatthias.ringwald break; 12682718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 12692718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 12702718e2e7Smatthias.ringwald break; 12712718e2e7Smatthias.ringwald default: 12722718e2e7Smatthias.ringwald break; 12732718e2e7Smatthias.ringwald } 12741eb2563eS[email protected] l2cap_run(); 12752718e2e7Smatthias.ringwald } 127600d93d79Smatthias.ringwald 127715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 127827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1279f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1280f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1281f62db1e3Smatthias.ringwald // discard channel 1282f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1283d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1284c8e4258aSmatthias.ringwald } 12851e6aba47Smatthias.ringwald 12869d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 1287c52bf64dSmatthias.ringwald linked_item_t *it; 12889d9bbc01Smatthias.ringwald 12899d9bbc01Smatthias.ringwald // close open channels 12909d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 12919d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 12929d9bbc01Smatthias.ringwald if ( service->psm == psm){ 12939d9bbc01Smatthias.ringwald return service; 12949d9bbc01Smatthias.ringwald }; 12959d9bbc01Smatthias.ringwald } 12969d9bbc01Smatthias.ringwald return NULL; 12979d9bbc01Smatthias.ringwald } 12989d9bbc01Smatthias.ringwald 129962f901dfS[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){ 1300e0abb8e7S[email protected] 1301e0abb8e7S[email protected] log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1302e0abb8e7S[email protected] 13034bb582b6Smatthias.ringwald // check for alread registered psm 13044bb582b6Smatthias.ringwald // TODO: emit error event 13059d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1306277abc2cSmatthias.ringwald if (service) { 1307277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: PSM %u already registered\n", psm); 130881476041Smatthias.ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 1309277abc2cSmatthias.ringwald return; 1310277abc2cSmatthias.ringwald } 13119d9bbc01Smatthias.ringwald 13124bb582b6Smatthias.ringwald // alloc structure 13134bb582b6Smatthias.ringwald // TODO: emit error event 131428ca2b46S[email protected] service = (l2cap_service_t *) btstack_memory_l2cap_service_get(); 1315277abc2cSmatthias.ringwald if (!service) { 1316277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n"); 13175842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 1318277abc2cSmatthias.ringwald return; 1319277abc2cSmatthias.ringwald } 13209d9bbc01Smatthias.ringwald 13219d9bbc01Smatthias.ringwald // fill in 13229d9bbc01Smatthias.ringwald service->psm = psm; 13239d9bbc01Smatthias.ringwald service->mtu = mtu; 13249d9bbc01Smatthias.ringwald service->connection = connection; 1325d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 1326df3354fcS[email protected] service->required_security_level = security_level; 13279d9bbc01Smatthias.ringwald 13289d9bbc01Smatthias.ringwald // add to services list 13299d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 1330c0e866bfSmatthias.ringwald 1331c0e866bfSmatthias.ringwald // enable page scan 1332c0e866bfSmatthias.ringwald hci_connectable_control(1); 13335842b6d9Smatthias.ringwald 13345842b6d9Smatthias.ringwald // done 13355842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, 0, psm); 13369d9bbc01Smatthias.ringwald } 13379d9bbc01Smatthias.ringwald 133836944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 1339e0abb8e7S[email protected] 1340e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1341e0abb8e7S[email protected] 13429d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1343037d6e48Smatthias.ringwald if (!service) return; 13449d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 1345d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1346c0e866bfSmatthias.ringwald 1347c0e866bfSmatthias.ringwald // disable page scan when no services registered 1348c0e866bfSmatthias.ringwald if (!linked_list_empty(&l2cap_services)) return; 1349c0e866bfSmatthias.ringwald hci_connectable_control(0); 13509d9bbc01Smatthias.ringwald } 13519d9bbc01Smatthias.ringwald 13529d9bbc01Smatthias.ringwald // 135336944dffSmatthias.ringwald void l2cap_close_connection(void *connection){ 13549d9bbc01Smatthias.ringwald linked_item_t *it; 13559d9bbc01Smatthias.ringwald 13563a9e0a58Smatthias.ringwald // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks 1357c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 1358c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1359c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 1360c52bf64dSmatthias.ringwald if (channel->connection == connection) { 1361cb8eb7e6Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1362c52bf64dSmatthias.ringwald } 1363c52bf64dSmatthias.ringwald } 13649d9bbc01Smatthias.ringwald 1365645658c9Smatthias.ringwald // unregister services 136669025de8Smatthias.ringwald it = (linked_item_t *) &l2cap_services; 136769025de8Smatthias.ringwald while (it->next) { 136869025de8Smatthias.ringwald l2cap_service_t * service = (l2cap_service_t *) it->next; 1369645658c9Smatthias.ringwald if (service->connection == connection){ 137069025de8Smatthias.ringwald it->next = it->next->next; 1371d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 13728149d2c7Smatthias.ringwald } else { 13738149d2c7Smatthias.ringwald it = it->next; 1374645658c9Smatthias.ringwald } 1375645658c9Smatthias.ringwald } 1376cb8eb7e6Smatthias.ringwald 1377cb8eb7e6Smatthias.ringwald // process 1378cb8eb7e6Smatthias.ringwald l2cap_run(); 1379c52bf64dSmatthias.ringwald } 13805652b5ffS[email protected] 13815652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 13825652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 13835652b5ffS[email protected] switch(channel_id){ 13845652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 13855652b5ffS[email protected] attribute_protocol_packet_handler = packet_handler; 13865652b5ffS[email protected] break; 13875652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 13885652b5ffS[email protected] security_protocol_packet_handler = packet_handler; 13895652b5ffS[email protected] break; 13905652b5ffS[email protected] } 13915652b5ffS[email protected] } 13925652b5ffS[email protected] 1393ab2b01dcS[email protected] #ifdef HAVE_BLE 1394ab2b01dcS[email protected] // Request LE connection parameter update 1395ab2b01dcS[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){ 1396ab2b01dcS[email protected] if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 1397ab2b01dcS[email protected] log_info("l2cap_send_signaling_packet, cannot send\n"); 1398ab2b01dcS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 1399ab2b01dcS[email protected] } 1400ab2b01dcS[email protected] // log_info("l2cap_send_signaling_packet type %u\n", cmd); 1401ab2b01dcS[email protected] uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 1402ab2b01dcS[email protected] uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier); 1403ab2b01dcS[email protected] return hci_send_acl_packet(acl_buffer, len); 1404ab2b01dcS[email protected] } 1405ab2b01dcS[email protected] #endif 1406ab2b01dcS[email protected] 1407