143625864Smatthias.ringwald /* 21713bceaSmatthias.ringwald * Copyright (C) 2009 by Matthias Ringwald 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 161713bceaSmatthias.ringwald * 171713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 181713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 191713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 201713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 211713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 221713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 231713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 241713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 251713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 261713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 271713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 281713bceaSmatthias.ringwald * SUCH DAMAGE. 291713bceaSmatthias.ringwald * 301713bceaSmatthias.ringwald */ 311713bceaSmatthias.ringwald 321713bceaSmatthias.ringwald /* 3343625864Smatthias.ringwald * l2cap.c 3443625864Smatthias.ringwald * 3543625864Smatthias.ringwald * Logical Link Control and Adaption Protocl (L2CAP) 3643625864Smatthias.ringwald * 3743625864Smatthias.ringwald * Created by Matthias Ringwald on 5/16/09. 3843625864Smatthias.ringwald */ 3943625864Smatthias.ringwald 4043625864Smatthias.ringwald #include "l2cap.h" 41645658c9Smatthias.ringwald #include "hci.h" 422b3c6c9bSmatthias.ringwald #include "hci_dump.h" 436218e6f1Smatthias.ringwald #include "debug.h" 44d3a9df87Smatthias.ringwald #include "btstack_memory.h" 4543625864Smatthias.ringwald 4643625864Smatthias.ringwald #include <stdarg.h> 4743625864Smatthias.ringwald #include <string.h> 4843625864Smatthias.ringwald 4943625864Smatthias.ringwald #include <stdio.h> 5043625864Smatthias.ringwald 51ea54feb7Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets (8) 52ea54feb7Smatthias.ringwald #define COMPLETE_L2CAP_HEADER (HCI_ACL_DATA_PKT_HDR + L2CAP_HEADER_SIZE) 536f60b3f4Smatthias.ringwald 542784b77dSmatthias.ringwald // minimum signaling MTU 552784b77dSmatthias.ringwald #define L2CAP_MINIMAL_MTU 48 561dc511deSmatthias.ringwald #define L2CAP_DEFAULT_MTU 672 572784b77dSmatthias.ringwald 58*816c0598Smatthias.ringwald // determine size of outgoing packet construction buffer - this includes ACL + L2CAP header 59*816c0598Smatthias.ringwald #if (L2CAP_MINIMAL_MTU + L2CAP_HEADER_SIZE) > (HCI_ACL_BUFFER_SIZE) 60*816c0598Smatthias.ringwald #define L2CAP_PACKET_BUFFER_SIZE (HCI_ACL_DATA_PKT_HDR + L2CAP_HEADER_SIZE + L2CAP_MINIMAL_MTU) 61*816c0598Smatthias.ringwald #else 62*816c0598Smatthias.ringwald #define L2CAP_PACKET_BUFFER_SIZE (HCI_ACL_DATA_PKT_HDR + HCI_ACL_BUFFER_SIZE) 63*816c0598Smatthias.ringwald #endif 64*816c0598Smatthias.ringwald 65*816c0598Smatthias.ringwald 664c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 674c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 684c744e21Smatthias.ringwald 6939bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 702b360848Smatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 5 7139bda6d5Smatthias.ringwald 7200d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 7300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 7400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 7500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 7600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 7700d93d79Smatthias.ringwald 7839bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 7939bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 8039bda6d5Smatthias.ringwald 8139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 822b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 832b83fb7dSmatthias.ringwald static int signaling_responses_pending; 842b83fb7dSmatthias.ringwald 85d3a9df87Smatthias.ringwald // static buffers 86*816c0598Smatthias.ringwald static uint8_t acl_buffer[L2CAP_PACKET_BUFFER_SIZE]; 87d3a9df87Smatthias.ringwald 881e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL; 899d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL; 9036944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 91808a48abSmatthias.ringwald static int new_credits_blocked = 0; 921e6aba47Smatthias.ringwald 9339bda6d5Smatthias.ringwald // prototypes 94fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 95fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm); 96fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 97fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 98fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 99fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 10039bda6d5Smatthias.ringwald 10139bda6d5Smatthias.ringwald 1021e6aba47Smatthias.ringwald void l2cap_init(){ 103fcadd0caSmatthias.ringwald 104808a48abSmatthias.ringwald new_credits_blocked = 0; 1052b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 106808a48abSmatthias.ringwald 107fcadd0caSmatthias.ringwald // 1082718e2e7Smatthias.ringwald // register callback with HCI 109fcadd0caSmatthias.ringwald // 1102718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 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) { 1319893b714Smatthias.ringwald uint8_t event[21]; 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); 14258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 14358de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 14458de5610Smatthias.ringwald } 14558de5610Smatthias.ringwald 14658de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 14758de5610Smatthias.ringwald uint8_t event[4]; 14858de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 14958de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 15058de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 15158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 15258de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 15358de5610Smatthias.ringwald } 15458de5610Smatthias.ringwald 15558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 15658de5610Smatthias.ringwald uint8_t event[16]; 15758de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 15858de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 15958de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 16058de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 16158de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 16258de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 16358de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 16458de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 16558de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1660af41d30Smatthias.ringwald } 167808a48abSmatthias.ringwald 1686218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 1696218e6f1Smatthias.ringwald // track credits 1706218e6f1Smatthias.ringwald channel->packets_granted += credits; 1717b5fbe1fSmatthias.ringwald // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits); 1726218e6f1Smatthias.ringwald 1736218e6f1Smatthias.ringwald uint8_t event[5]; 1746218e6f1Smatthias.ringwald event[0] = L2CAP_EVENT_CREDITS; 1756218e6f1Smatthias.ringwald event[1] = sizeof(event) - 2; 1766218e6f1Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 1776218e6f1Smatthias.ringwald event[4] = credits; 1786218e6f1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1796218e6f1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1806218e6f1Smatthias.ringwald } 1816218e6f1Smatthias.ringwald 182808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){ 183808a48abSmatthias.ringwald new_credits_blocked = blocked; 184808a48abSmatthias.ringwald } 185808a48abSmatthias.ringwald 18640d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){ 187808a48abSmatthias.ringwald 188808a48abSmatthias.ringwald if (new_credits_blocked) return; // we're told not to. used by daemon 189808a48abSmatthias.ringwald 1908d371091Smatthias.ringwald linked_item_t *it; 1918d371091Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1928d371091Smatthias.ringwald if (!hci_number_free_acl_slots()) return; 1938d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 1940e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 1954c744e21Smatthias.ringwald if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 1968d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 1978d371091Smatthias.ringwald } 1988d371091Smatthias.ringwald } 1998d371091Smatthias.ringwald } 2008d371091Smatthias.ringwald 201b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 202f62db1e3Smatthias.ringwald linked_item_t *it; 203f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 2048d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 205b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 206f62db1e3Smatthias.ringwald return channel; 207f62db1e3Smatthias.ringwald } 208f62db1e3Smatthias.ringwald } 209f62db1e3Smatthias.ringwald return NULL; 210f62db1e3Smatthias.ringwald } 211f62db1e3Smatthias.ringwald 2126b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2136b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2146b1fde37Smatthias.ringwald if (!channel) return 0; 2156b1fde37Smatthias.ringwald if (!channel->packets_granted) return 0; 2166b1fde37Smatthias.ringwald return hci_can_send_packet_now(HCI_ACL_DATA_PACKET); 2176b1fde37Smatthias.ringwald } 2186b1fde37Smatthias.ringwald 21996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 22096cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 22196cbd662Smatthias.ringwald if (channel) { 22296cbd662Smatthias.ringwald return channel->remote_mtu; 22396cbd662Smatthias.ringwald } 22496cbd662Smatthias.ringwald return 0; 22596cbd662Smatthias.ringwald } 22696cbd662Smatthias.ringwald 22758de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 2287b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet type %u\n", cmd); 22958de5610Smatthias.ringwald va_list argptr; 23058de5610Smatthias.ringwald va_start(argptr, identifier); 231*816c0598Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr); 23258de5610Smatthias.ringwald va_end(argptr); 2337b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet con %u!\n", handle); 234*816c0598Smatthias.ringwald return hci_send_acl_packet(acl_buffer, len); 23558de5610Smatthias.ringwald } 23658de5610Smatthias.ringwald 2376218e6f1Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 2386218e6f1Smatthias.ringwald 2396218e6f1Smatthias.ringwald // check for free places on BT module 2408ea03fa5Smatthias.ringwald if (!hci_number_free_acl_slots()) { 2417b5fbe1fSmatthias.ringwald log_info("l2cap_send_internal cid %u, BT module full <-----\n", local_cid); 242808a48abSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 2438ea03fa5Smatthias.ringwald } 2446218e6f1Smatthias.ringwald int err = 0; 2456218e6f1Smatthias.ringwald 24658de5610Smatthias.ringwald // find channel for local_cid, construct l2cap packet and send 24758de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 24858de5610Smatthias.ringwald if (channel) { 2496218e6f1Smatthias.ringwald if (channel->packets_granted > 0){ 2506218e6f1Smatthias.ringwald --channel->packets_granted; 2517b5fbe1fSmatthias.ringwald // log_info("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n", 252808a48abSmatthias.ringwald // local_cid, channel->handle, channel->packets_granted); 2536218e6f1Smatthias.ringwald } else { 2547d67539fSmatthias.ringwald log_error("l2cap_send_internal cid %u, no credits!\n", local_cid); 2556218e6f1Smatthias.ringwald } 2566218e6f1Smatthias.ringwald 25758de5610Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 25858de5610Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 25958de5610Smatthias.ringwald // 2 - ACL length 26058de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 26158de5610Smatthias.ringwald // 4 - L2CAP packet length 26258de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 26358de5610Smatthias.ringwald // 6 - L2CAP channel DEST 26458de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 26558de5610Smatthias.ringwald // 8 - data 26658de5610Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 2676218e6f1Smatthias.ringwald 26858de5610Smatthias.ringwald // send 2696218e6f1Smatthias.ringwald err = hci_send_acl_packet(acl_buffer, len+8); 27058de5610Smatthias.ringwald } 27191b99603Smatthias.ringwald 27291b99603Smatthias.ringwald l2cap_hand_out_credits(); 27391b99603Smatthias.ringwald 2746218e6f1Smatthias.ringwald return err; 27558de5610Smatthias.ringwald } 27658de5610Smatthias.ringwald 2778158c421Smatthias.ringwald // MARK: L2CAP_RUN 2782cd0be45Smatthias.ringwald // process outstanding signaling tasks 2792cd0be45Smatthias.ringwald void l2cap_run(void){ 2802b83fb7dSmatthias.ringwald 2812b83fb7dSmatthias.ringwald // check pending signaling responses 2822b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 2832b83fb7dSmatthias.ringwald 28402b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 2852b83fb7dSmatthias.ringwald 2862b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 2872b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 2882b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 2892b360848Smatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST 2902b83fb7dSmatthias.ringwald 2912b83fb7dSmatthias.ringwald switch (signaling_responses[0].code){ 2922b360848Smatthias.ringwald case CONNECTION_REQUEST: 2932b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 2942b360848Smatthias.ringwald break; 2952b83fb7dSmatthias.ringwald case ECHO_REQUEST: 2962b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 2972b83fb7dSmatthias.ringwald break; 2982b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 2992b83fb7dSmatthias.ringwald if (infoType == 2) { 3002b83fb7dSmatthias.ringwald uint32_t features = 0; 3012b83fb7dSmatthias.ringwald // extended features request supported, however no features present 3022b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 3032b83fb7dSmatthias.ringwald } else { 3042b83fb7dSmatthias.ringwald // all other types are not supported 3052b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 3062b83fb7dSmatthias.ringwald } 3072b83fb7dSmatthias.ringwald break; 3082b83fb7dSmatthias.ringwald default: 3092b83fb7dSmatthias.ringwald // should not happen 3102b83fb7dSmatthias.ringwald break; 3112b83fb7dSmatthias.ringwald } 3122b83fb7dSmatthias.ringwald 3132b83fb7dSmatthias.ringwald // remove first item 3142b83fb7dSmatthias.ringwald signaling_responses_pending--; 3152b83fb7dSmatthias.ringwald int i; 3162b83fb7dSmatthias.ringwald for (i=0; i < signaling_responses_pending; i++){ 3172b83fb7dSmatthias.ringwald signaling_responses[i] = signaling_responses[i+1]; 3182b83fb7dSmatthias.ringwald } 3192b83fb7dSmatthias.ringwald } 3202b83fb7dSmatthias.ringwald 321ae280e73Smatthias.ringwald uint8_t config_options[4]; 3222cd0be45Smatthias.ringwald linked_item_t *it; 3232cd0be45Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 3242cd0be45Smatthias.ringwald 32502b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 32602b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 3272cd0be45Smatthias.ringwald 3282cd0be45Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 3296e6710ebSmatthias.ringwald 3307b5fbe1fSmatthias.ringwald // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 3316e6710ebSmatthias.ringwald 3322cd0be45Smatthias.ringwald switch (channel->state){ 3332cd0be45Smatthias.ringwald 33402b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 33564472d52Smatthias.ringwald // send connection request - set state first 33664472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 33702b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 3388f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 33902b22dc4Smatthias.ringwald break; 34002b22dc4Smatthias.ringwald 341e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 342b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0); 343e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 344e7ff783cSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 345d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 346e7ff783cSmatthias.ringwald break; 347e7ff783cSmatthias.ringwald 348552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 349fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 35073cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 3512a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 352552d92a1Smatthias.ringwald break; 353552d92a1Smatthias.ringwald 3546fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 3556fdcc387Smatthias.ringwald // success, start l2cap handshake 356b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 3576fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 3582a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 3596fdcc387Smatthias.ringwald break; 3606fdcc387Smatthias.ringwald 361fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 36273cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 36373cf2b3dSmatthias.ringwald channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP; 36473cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP; 365fa8473a4Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL); 366fa8473a4Smatthias.ringwald } 36773cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 36873cf2b3dSmatthias.ringwald channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 36973cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ; 370b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 371ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 372ae280e73Smatthias.ringwald config_options[1] = 2; // len param 373ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 374b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 375fa8473a4Smatthias.ringwald } 376fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 377552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 378552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 379552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 380fa8473a4Smatthias.ringwald } 381552d92a1Smatthias.ringwald break; 382552d92a1Smatthias.ringwald 383e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 384b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 385e7ff783cSmatthias.ringwald l2cap_finialize_channel_close(channel); 386e7ff783cSmatthias.ringwald break; 387e7ff783cSmatthias.ringwald 388e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 389b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 3902cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 3912a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 3922cd0be45Smatthias.ringwald break; 3932cd0be45Smatthias.ringwald default: 3942cd0be45Smatthias.ringwald break; 3952cd0be45Smatthias.ringwald } 3962cd0be45Smatthias.ringwald } 3972cd0be45Smatthias.ringwald } 3982cd0be45Smatthias.ringwald 399fa8c92f6Smatthias.ringwald static uint16_t l2cap_max_l2cap_mtu(void){ 400*816c0598Smatthias.ringwald return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE; 401fa8c92f6Smatthias.ringwald } 402fa8c92f6Smatthias.ringwald 4031e6aba47Smatthias.ringwald // open outgoing L2CAP channel 40415470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, 40515470d27Smatthias.ringwald bd_addr_t address, uint16_t psm, uint16_t mtu){ 4061e6aba47Smatthias.ringwald 4071e6aba47Smatthias.ringwald // alloc structure 408d3a9df87Smatthias.ringwald l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 4092b360848Smatthias.ringwald if (!chan) { 4102b360848Smatthias.ringwald // emit error event 4112b360848Smatthias.ringwald l2cap_channel_t dummy_channel; 4122b360848Smatthias.ringwald BD_ADDR_COPY(dummy_channel.address, address); 4132b360848Smatthias.ringwald dummy_channel.psm = psm; 4142b360848Smatthias.ringwald l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 4152b360848Smatthias.ringwald return; 4162b360848Smatthias.ringwald } 4171d279b20Smatthias.ringwald // limit local mtu to max acl packet length 418fa8c92f6Smatthias.ringwald if (mtu > l2cap_max_l2cap_mtu()) { 419fa8c92f6Smatthias.ringwald mtu = l2cap_max_l2cap_mtu(); 4209775e25bSmatthias.ringwald } 4219775e25bSmatthias.ringwald 4221e6aba47Smatthias.ringwald // fill in 4231e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 4241e6aba47Smatthias.ringwald chan->psm = psm; 4251e6aba47Smatthias.ringwald chan->handle = 0; 4261e6aba47Smatthias.ringwald chan->connection = connection; 4276b296a27Smatthias.ringwald chan->packet_handler = packet_handler; 4282784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 42915470d27Smatthias.ringwald chan->local_mtu = mtu; 4306218e6f1Smatthias.ringwald chan->packets_granted = 0; 4316218e6f1Smatthias.ringwald 4321e6aba47Smatthias.ringwald // set initial state 43302b22dc4Smatthias.ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 43473cf2b3dSmatthias.ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 435b1988dceSmatthias.ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 436b1988dceSmatthias.ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 4371e6aba47Smatthias.ringwald 4381e6aba47Smatthias.ringwald // add to connections list 4391e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 4401e6aba47Smatthias.ringwald 44102b22dc4Smatthias.ringwald l2cap_run(); 44243625864Smatthias.ringwald } 44343625864Smatthias.ringwald 444b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 445b35f641cSmatthias.ringwald // find channel for local_cid 446b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 447f62db1e3Smatthias.ringwald if (channel) { 448e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 449f62db1e3Smatthias.ringwald } 4502cd0be45Smatthias.ringwald // process 4512cd0be45Smatthias.ringwald l2cap_run(); 45243625864Smatthias.ringwald } 4531e6aba47Smatthias.ringwald 454afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 45515ec09bbSmatthias.ringwald linked_item_t *it = (linked_item_t *) &l2cap_channels; 45615ec09bbSmatthias.ringwald while (it->next){ 45715ec09bbSmatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 458b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 45902b22dc4Smatthias.ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 460afde0c52Smatthias.ringwald // failure, forward error code 461afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 462afde0c52Smatthias.ringwald // discard channel 46315ec09bbSmatthias.ringwald it->next = it->next->next; 464d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 465afde0c52Smatthias.ringwald } 46615ec09bbSmatthias.ringwald } else { 46715ec09bbSmatthias.ringwald it = it->next; 468afde0c52Smatthias.ringwald } 469afde0c52Smatthias.ringwald } 470afde0c52Smatthias.ringwald } 471afde0c52Smatthias.ringwald 472afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 473afde0c52Smatthias.ringwald linked_item_t *it; 474afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 475afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 476afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 47702b22dc4Smatthias.ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 478b448a0e7Smatthias.ringwald // success, start l2cap handshake 4796fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 480afde0c52Smatthias.ringwald channel->handle = handle; 481b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 482afde0c52Smatthias.ringwald } 483afde0c52Smatthias.ringwald } 484afde0c52Smatthias.ringwald } 4856fdcc387Smatthias.ringwald // process 4866fdcc387Smatthias.ringwald l2cap_run(); 487afde0c52Smatthias.ringwald } 488b448a0e7Smatthias.ringwald 489afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 490afde0c52Smatthias.ringwald 491afde0c52Smatthias.ringwald bd_addr_t address; 492afde0c52Smatthias.ringwald hci_con_handle_t handle; 4936e6710ebSmatthias.ringwald l2cap_channel_t * channel; 49436944dffSmatthias.ringwald linked_item_t *it; 4952d00edd4Smatthias.ringwald int hci_con_used; 496afde0c52Smatthias.ringwald 497afde0c52Smatthias.ringwald switch(packet[0]){ 498afde0c52Smatthias.ringwald 499afde0c52Smatthias.ringwald // handle connection complete events 500afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 501afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 502afde0c52Smatthias.ringwald if (packet[2] == 0){ 503afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 504afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 505afde0c52Smatthias.ringwald } else { 506afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 507afde0c52Smatthias.ringwald } 508afde0c52Smatthias.ringwald break; 509afde0c52Smatthias.ringwald 510afde0c52Smatthias.ringwald // handle successful create connection cancel command 511afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 512afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 513afde0c52Smatthias.ringwald if (packet[5] == 0){ 514afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 515afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 516afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 51703cfbabcSmatthias.ringwald } 5181e6aba47Smatthias.ringwald } 51939d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 52039d59809Smatthias.ringwald break; 52139d59809Smatthias.ringwald 52239d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 52339d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 524afde0c52Smatthias.ringwald break; 52527a923d0Smatthias.ringwald 5261e6aba47Smatthias.ringwald // handle disconnection complete events 527afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 52827a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 529afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 53015ec09bbSmatthias.ringwald it = (linked_item_t *) &l2cap_channels; 53115ec09bbSmatthias.ringwald while (it->next){ 53239ce35a7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 53327a923d0Smatthias.ringwald if ( channel->handle == handle ){ 53415ec09bbSmatthias.ringwald // update prev item before free'ing next element - don't call l2cap_finalize_channel_close 5352060cf14Smatthias.ringwald it->next = it->next->next; 53615ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 537d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 53815ec09bbSmatthias.ringwald } else { 53915ec09bbSmatthias.ringwald it = it->next; 54027a923d0Smatthias.ringwald } 54127a923d0Smatthias.ringwald } 542afde0c52Smatthias.ringwald break; 543fcadd0caSmatthias.ringwald 5446218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 54502b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 5468d371091Smatthias.ringwald l2cap_hand_out_credits(); 5476218e6f1Smatthias.ringwald break; 5486218e6f1Smatthias.ringwald 549ee091cf1Smatthias.ringwald // HCI Connection Timeouts 550afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 55136944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 55280ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 5532d00edd4Smatthias.ringwald hci_con_used = 0; 554ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 555ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 556ee091cf1Smatthias.ringwald if (channel->handle == handle) { 5572d00edd4Smatthias.ringwald hci_con_used = 1; 558ee091cf1Smatthias.ringwald } 559ee091cf1Smatthias.ringwald } 5602d00edd4Smatthias.ringwald if (hci_con_used) break; 56132ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 5629edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 563afde0c52Smatthias.ringwald break; 564ee091cf1Smatthias.ringwald 5656e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 5666e6710ebSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 5676e6710ebSmatthias.ringwald channel = (l2cap_channel_t *) it; 5686e6710ebSmatthias.ringwald if (channel->packet_handler) { 5696e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 5706e6710ebSmatthias.ringwald } 5716e6710ebSmatthias.ringwald } 5726e6710ebSmatthias.ringwald break; 5736e6710ebSmatthias.ringwald 574afde0c52Smatthias.ringwald default: 575afde0c52Smatthias.ringwald break; 576afde0c52Smatthias.ringwald } 577afde0c52Smatthias.ringwald 578afde0c52Smatthias.ringwald // pass on 579b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 5801e6aba47Smatthias.ringwald } 5811e6aba47Smatthias.ringwald 582afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 583b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 584e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 585e7ff783cSmatthias.ringwald l2cap_run(); 58684836b65Smatthias.ringwald } 58784836b65Smatthias.ringwald 5882b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 5892b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 5902b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 5912b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 5922b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 5932b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 5942b360848Smatthias.ringwald signaling_responses_pending++; 5952b360848Smatthias.ringwald l2cap_run(); 5962b360848Smatthias.ringwald } 5972b360848Smatthias.ringwald } 5982b360848Smatthias.ringwald 599b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 600645658c9Smatthias.ringwald 6017b5fbe1fSmatthias.ringwald // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 602645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 603645658c9Smatthias.ringwald if (!service) { 604645658c9Smatthias.ringwald // 0x0002 PSM not supported 6052b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 606645658c9Smatthias.ringwald return; 607645658c9Smatthias.ringwald } 608645658c9Smatthias.ringwald 609645658c9Smatthias.ringwald hci_connection_t * hci_connection = connection_for_handle( handle ); 610645658c9Smatthias.ringwald if (!hci_connection) { 6112b360848Smatthias.ringwald // 6127d67539fSmatthias.ringwald log_error("no hci_connection for handle %u\n", handle); 613645658c9Smatthias.ringwald return; 614645658c9Smatthias.ringwald } 615645658c9Smatthias.ringwald // alloc structure 6167b5fbe1fSmatthias.ringwald // log_info("l2cap_handle_connection_request register channel\n"); 617d3a9df87Smatthias.ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 6182b360848Smatthias.ringwald if (!channel){ 6192b360848Smatthias.ringwald // 0x0004 No resources available 6202b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 6212b360848Smatthias.ringwald return; 6222b360848Smatthias.ringwald } 623645658c9Smatthias.ringwald 624645658c9Smatthias.ringwald // fill in 625169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 626169f8b28Smatthias.ringwald channel->psm = psm; 627169f8b28Smatthias.ringwald channel->handle = handle; 628169f8b28Smatthias.ringwald channel->connection = service->connection; 629f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 630b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 631b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 632fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 6330a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 634761b0451Smatthias.ringwald channel->packets_granted = 0; 635b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 636645658c9Smatthias.ringwald 6371d279b20Smatthias.ringwald // limit local mtu to max acl packet length 638fa8c92f6Smatthias.ringwald if (channel->local_mtu > l2cap_max_l2cap_mtu()) { 639fa8c92f6Smatthias.ringwald channel->local_mtu = l2cap_max_l2cap_mtu(); 6409775e25bSmatthias.ringwald } 6419775e25bSmatthias.ringwald 642645658c9Smatthias.ringwald // set initial state 643e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 64473cf2b3dSmatthias.ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 645e405ae81Smatthias.ringwald 646645658c9Smatthias.ringwald // add to connections list 647169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 648645658c9Smatthias.ringwald 649e405ae81Smatthias.ringwald // emit incoming connection request 650e405ae81Smatthias.ringwald l2cap_emit_connection_request(channel); 651e405ae81Smatthias.ringwald } 652645658c9Smatthias.ringwald 653b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 654b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 655e405ae81Smatthias.ringwald if (!channel) { 6567d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 657e405ae81Smatthias.ringwald return; 658e405ae81Smatthias.ringwald } 659e405ae81Smatthias.ringwald 660552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 661e405ae81Smatthias.ringwald 662552d92a1Smatthias.ringwald // process 663552d92a1Smatthias.ringwald l2cap_run(); 664e405ae81Smatthias.ringwald } 665645658c9Smatthias.ringwald 666b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 667b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 668e405ae81Smatthias.ringwald if (!channel) { 6697d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 670e405ae81Smatthias.ringwald return; 671e405ae81Smatthias.ringwald } 672e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 673e7ff783cSmatthias.ringwald channel->reason = reason; 674e7ff783cSmatthias.ringwald l2cap_run(); 675645658c9Smatthias.ringwald } 676645658c9Smatthias.ringwald 6772784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 678b1988dceSmatthias.ringwald 679b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 680b1988dceSmatthias.ringwald 6812784b77dSmatthias.ringwald // accept the other's configuration options 6823de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 6833de7c0caSmatthias.ringwald uint16_t pos = 8; 6843de7c0caSmatthias.ringwald while (pos < end_pos){ 6851dc511deSmatthias.ringwald uint8_t type = command[pos++]; 6861dc511deSmatthias.ringwald uint8_t length = command[pos++]; 6871dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 6881dc511deSmatthias.ringwald if ((type & 0x7f) == 1 && length == 2){ 6891dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 6907b5fbe1fSmatthias.ringwald // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 6911dc511deSmatthias.ringwald } 6921dc511deSmatthias.ringwald pos += length; 6931dc511deSmatthias.ringwald } 6942784b77dSmatthias.ringwald } 6952784b77dSmatthias.ringwald 696fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 6977b5fbe1fSmatthias.ringwald // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var); 69873cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 69973cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 700fa8473a4Smatthias.ringwald return 1; 701fa8473a4Smatthias.ringwald } 702fa8473a4Smatthias.ringwald 703fa8473a4Smatthias.ringwald 70400d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 7051e6aba47Smatthias.ringwald 70600d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 70700d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 70838e5900eSmatthias.ringwald uint16_t result = 0; 7091e6aba47Smatthias.ringwald 7107b5fbe1fSmatthias.ringwald log_info("signaling handler code %u, state %u\n", code, channel->state); 711b35f641cSmatthias.ringwald 7129a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 7139a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 7149a011532Smatthias.ringwald switch (channel->state){ 715fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 7169a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 7172b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 7189a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 7199a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 7209a011532Smatthias.ringwald break; 7219a011532Smatthias.ringwald 7229a011532Smatthias.ringwald default: 7239a011532Smatthias.ringwald // ignore in other states 7249a011532Smatthias.ringwald break; 7259a011532Smatthias.ringwald } 7269a011532Smatthias.ringwald return; 7279a011532Smatthias.ringwald } 7289a011532Smatthias.ringwald 7291e6aba47Smatthias.ringwald switch (channel->state) { 7301e6aba47Smatthias.ringwald 7311e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 7321e6aba47Smatthias.ringwald switch (code){ 7331e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 73400d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 73538e5900eSmatthias.ringwald switch (result) { 73638e5900eSmatthias.ringwald case 0: 737169f8b28Smatthias.ringwald // successful connection 73800d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 739fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 74073cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 74138e5900eSmatthias.ringwald break; 74238e5900eSmatthias.ringwald case 1: 74338e5900eSmatthias.ringwald // connection pending. get some coffee 74438e5900eSmatthias.ringwald break; 74538e5900eSmatthias.ringwald default: 746eb920dbeSmatthias.ringwald // channel closed 747eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 748eb920dbeSmatthias.ringwald 749f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 75038e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 751eb920dbeSmatthias.ringwald 752eb920dbeSmatthias.ringwald // drop link key if security block 753eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 754eb920dbeSmatthias.ringwald hci_drop_link_key_for_bd_addr(&channel->address); 755eb920dbeSmatthias.ringwald } 756eb920dbeSmatthias.ringwald 757eb920dbeSmatthias.ringwald // discard channel 758eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 759d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 76038e5900eSmatthias.ringwald break; 7611e6aba47Smatthias.ringwald } 7621e6aba47Smatthias.ringwald break; 76338e5900eSmatthias.ringwald 76438e5900eSmatthias.ringwald default: 7651e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 76638e5900eSmatthias.ringwald break; 7671e6aba47Smatthias.ringwald } 7681e6aba47Smatthias.ringwald break; 7691e6aba47Smatthias.ringwald 770fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 771ae280e73Smatthias.ringwald switch (code) { 772ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 77373cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ; 77473cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP; 775ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 776ae280e73Smatthias.ringwald break; 7771e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 77873cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP; 7795a67bd4aSmatthias.ringwald break; 7805a67bd4aSmatthias.ringwald default: 7815a67bd4aSmatthias.ringwald break; 7821e6aba47Smatthias.ringwald } 783fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 784fa8473a4Smatthias.ringwald // for open: 7855a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 786fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 7876218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 788c8e4258aSmatthias.ringwald } 789c8e4258aSmatthias.ringwald break; 790f62db1e3Smatthias.ringwald 791f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 792f62db1e3Smatthias.ringwald switch (code) { 793f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 79427a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 79527a923d0Smatthias.ringwald break; 7965a67bd4aSmatthias.ringwald default: 7975a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 7985a67bd4aSmatthias.ringwald break; 79927a923d0Smatthias.ringwald } 80027a923d0Smatthias.ringwald break; 80184836b65Smatthias.ringwald 80284836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 80384836b65Smatthias.ringwald // @TODO handle incoming requests 80484836b65Smatthias.ringwald break; 80584836b65Smatthias.ringwald 80684836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 80784836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 80884836b65Smatthias.ringwald break; 80910642e45Smatthias.ringwald default: 81010642e45Smatthias.ringwald break; 81127a923d0Smatthias.ringwald } 8127b5fbe1fSmatthias.ringwald // log_info("new state %u\n", channel->state); 81327a923d0Smatthias.ringwald } 81427a923d0Smatthias.ringwald 81500d93d79Smatthias.ringwald 81600d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 81700d93d79Smatthias.ringwald 81800d93d79Smatthias.ringwald // get code, signalind identifier and command len 81900d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 82000d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 82100d93d79Smatthias.ringwald 82200d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 82300d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 82400d93d79Smatthias.ringwald return; 82500d93d79Smatthias.ringwald } 82600d93d79Smatthias.ringwald 82700d93d79Smatthias.ringwald // general commands without an assigned channel 82800d93d79Smatthias.ringwald switch(code) { 82900d93d79Smatthias.ringwald 83000d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 83100d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 83200d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 83300d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 8342b83fb7dSmatthias.ringwald return; 83500d93d79Smatthias.ringwald } 83600d93d79Smatthias.ringwald 8372b360848Smatthias.ringwald case ECHO_REQUEST: 8382b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 8392b83fb7dSmatthias.ringwald return; 84000d93d79Smatthias.ringwald 84100d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 84200d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 8432b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 8442b83fb7dSmatthias.ringwald return; 84500d93d79Smatthias.ringwald } 84600d93d79Smatthias.ringwald 84700d93d79Smatthias.ringwald default: 84800d93d79Smatthias.ringwald break; 84900d93d79Smatthias.ringwald } 85000d93d79Smatthias.ringwald 85100d93d79Smatthias.ringwald 85200d93d79Smatthias.ringwald // Get potential destination CID 85300d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 85400d93d79Smatthias.ringwald 85500d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 85600d93d79Smatthias.ringwald linked_item_t *it; 85700d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 85800d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 85900d93d79Smatthias.ringwald if (channel->handle == handle) { 86000d93d79Smatthias.ringwald if (code & 1) { 861b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 862b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 86300d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 8644e32727eSmatthias.ringwald break; 86500d93d79Smatthias.ringwald } 86600d93d79Smatthias.ringwald } else { 867b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 86800d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 86900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 8704e32727eSmatthias.ringwald break; 87100d93d79Smatthias.ringwald } 87200d93d79Smatthias.ringwald } 87300d93d79Smatthias.ringwald } 87400d93d79Smatthias.ringwald } 87500d93d79Smatthias.ringwald } 87600d93d79Smatthias.ringwald 87700d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 87800d93d79Smatthias.ringwald 87900d93d79Smatthias.ringwald // Get Channel ID 88000d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 88100d93d79Smatthias.ringwald 88200d93d79Smatthias.ringwald // Signaling Packet? 88300d93d79Smatthias.ringwald if (channel_id == 1) { 88400d93d79Smatthias.ringwald 88500d93d79Smatthias.ringwald // Get Connection 88600d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 88700d93d79Smatthias.ringwald 88800d93d79Smatthias.ringwald uint16_t command_offset = 8; 88900d93d79Smatthias.ringwald while (command_offset < size) { 89000d93d79Smatthias.ringwald 89100d93d79Smatthias.ringwald // handle signaling commands 89200d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 89300d93d79Smatthias.ringwald 89400d93d79Smatthias.ringwald // increment command_offset 89500d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 89600d93d79Smatthias.ringwald } 89764472d52Smatthias.ringwald 89864472d52Smatthias.ringwald l2cap_run(); 89964472d52Smatthias.ringwald 90000d93d79Smatthias.ringwald return; 90100d93d79Smatthias.ringwald } 90200d93d79Smatthias.ringwald 90300d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 90400d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 90500d93d79Smatthias.ringwald if (channel) { 90658de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 90700d93d79Smatthias.ringwald } 90800d93d79Smatthias.ringwald } 90900d93d79Smatthias.ringwald 9102718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 9112718e2e7Smatthias.ringwald switch (packet_type) { 9122718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 9132718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 9142718e2e7Smatthias.ringwald break; 9152718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 9162718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 9172718e2e7Smatthias.ringwald break; 9182718e2e7Smatthias.ringwald default: 9192718e2e7Smatthias.ringwald break; 9202718e2e7Smatthias.ringwald } 9212718e2e7Smatthias.ringwald } 92200d93d79Smatthias.ringwald 92315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 92427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 925f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 926f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 927f62db1e3Smatthias.ringwald // discard channel 928f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 929d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 930c8e4258aSmatthias.ringwald } 9311e6aba47Smatthias.ringwald 9329d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 933c52bf64dSmatthias.ringwald linked_item_t *it; 9349d9bbc01Smatthias.ringwald 9359d9bbc01Smatthias.ringwald // close open channels 9369d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 9379d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 9389d9bbc01Smatthias.ringwald if ( service->psm == psm){ 9399d9bbc01Smatthias.ringwald return service; 9409d9bbc01Smatthias.ringwald }; 9419d9bbc01Smatthias.ringwald } 9429d9bbc01Smatthias.ringwald return NULL; 9439d9bbc01Smatthias.ringwald } 9449d9bbc01Smatthias.ringwald 94536944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 9464bb582b6Smatthias.ringwald // check for alread registered psm 9474bb582b6Smatthias.ringwald // TODO: emit error event 9489d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 949277abc2cSmatthias.ringwald if (service) { 950277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: PSM %u already registered\n", psm); 951277abc2cSmatthias.ringwald return; 952277abc2cSmatthias.ringwald } 9539d9bbc01Smatthias.ringwald 9544bb582b6Smatthias.ringwald // alloc structure 9554bb582b6Smatthias.ringwald // TODO: emit error event 956d3a9df87Smatthias.ringwald service = btstack_memory_l2cap_service_get(); 957277abc2cSmatthias.ringwald if (!service) { 958277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n"); 959277abc2cSmatthias.ringwald return; 960277abc2cSmatthias.ringwald } 9619d9bbc01Smatthias.ringwald 9629d9bbc01Smatthias.ringwald // fill in 9639d9bbc01Smatthias.ringwald service->psm = psm; 9649d9bbc01Smatthias.ringwald service->mtu = mtu; 9659d9bbc01Smatthias.ringwald service->connection = connection; 966d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 9679d9bbc01Smatthias.ringwald 9689d9bbc01Smatthias.ringwald // add to services list 9699d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 9709d9bbc01Smatthias.ringwald } 9719d9bbc01Smatthias.ringwald 97236944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 9739d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 974037d6e48Smatthias.ringwald if (!service) return; 9759d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 976d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 9779d9bbc01Smatthias.ringwald } 9789d9bbc01Smatthias.ringwald 9799d9bbc01Smatthias.ringwald // 98036944dffSmatthias.ringwald void l2cap_close_connection(void *connection){ 9819d9bbc01Smatthias.ringwald linked_item_t *it; 9829d9bbc01Smatthias.ringwald 9833a9e0a58Smatthias.ringwald // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks 984c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 985c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 986c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 987c52bf64dSmatthias.ringwald if (channel->connection == connection) { 988cb8eb7e6Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 989c52bf64dSmatthias.ringwald } 990c52bf64dSmatthias.ringwald } 9919d9bbc01Smatthias.ringwald 992645658c9Smatthias.ringwald // unregister services 99369025de8Smatthias.ringwald it = (linked_item_t *) &l2cap_services; 99469025de8Smatthias.ringwald while (it->next) { 99569025de8Smatthias.ringwald l2cap_service_t * service = (l2cap_service_t *) it->next; 996645658c9Smatthias.ringwald if (service->connection == connection){ 99769025de8Smatthias.ringwald it->next = it->next->next; 998d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 9998149d2c7Smatthias.ringwald } else { 10008149d2c7Smatthias.ringwald it = it->next; 1001645658c9Smatthias.ringwald } 1002645658c9Smatthias.ringwald } 1003cb8eb7e6Smatthias.ringwald 1004cb8eb7e6Smatthias.ringwald // process 1005cb8eb7e6Smatthias.ringwald l2cap_run(); 1006c52bf64dSmatthias.ringwald } 1007c52bf64dSmatthias.ringwald