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 514c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 524c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 534c744e21Smatthias.ringwald 5439bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 552b360848Smatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 5 5639bda6d5Smatthias.ringwald 5700d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 5800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 5900d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 6000d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6100d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6200d93d79Smatthias.ringwald 6339bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 6439bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 6539bda6d5Smatthias.ringwald 6639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 672b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 682b83fb7dSmatthias.ringwald static int signaling_responses_pending; 692b83fb7dSmatthias.ringwald 701e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL; 719d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL; 7236944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 73808a48abSmatthias.ringwald static int new_credits_blocked = 0; 741e6aba47Smatthias.ringwald 7539bda6d5Smatthias.ringwald // prototypes 76fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 77fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm); 78fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 79fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 80fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 81fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 8239bda6d5Smatthias.ringwald 8339bda6d5Smatthias.ringwald 841e6aba47Smatthias.ringwald void l2cap_init(){ 85fcadd0caSmatthias.ringwald 86808a48abSmatthias.ringwald new_credits_blocked = 0; 872b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 88808a48abSmatthias.ringwald 89fcadd0caSmatthias.ringwald // 902718e2e7Smatthias.ringwald // register callback with HCI 91fcadd0caSmatthias.ringwald // 922718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 93fcadd0caSmatthias.ringwald } 94fcadd0caSmatthias.ringwald 95fcadd0caSmatthias.ringwald 96fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 9736944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 98fcadd0caSmatthias.ringwald } 9936944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 100b502e1b0Smatthias.ringwald packet_handler = handler; 1011e6aba47Smatthias.ringwald } 1021e6aba47Smatthias.ringwald 10358de5610Smatthias.ringwald // notify client/protocol handler 10458de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 10558de5610Smatthias.ringwald if (channel->packet_handler) { 10658de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 10758de5610Smatthias.ringwald } else { 10836944dffSmatthias.ringwald (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 10958de5610Smatthias.ringwald } 11058de5610Smatthias.ringwald } 11158de5610Smatthias.ringwald 11258de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 1139893b714Smatthias.ringwald uint8_t event[21]; 11458de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 11558de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 11658de5610Smatthias.ringwald event[2] = status; 11758de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 11858de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 11958de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 12058de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 12158de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 1224c98aa43Smatthias.ringwald bt_store_16(event, 17, channel->local_mtu); 1234c98aa43Smatthias.ringwald bt_store_16(event, 19, channel->remote_mtu); 12458de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 12558de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 12658de5610Smatthias.ringwald } 12758de5610Smatthias.ringwald 12858de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 12958de5610Smatthias.ringwald uint8_t event[4]; 13058de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 13158de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 13258de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 13358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 13458de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 13558de5610Smatthias.ringwald } 13658de5610Smatthias.ringwald 13758de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 13858de5610Smatthias.ringwald uint8_t event[16]; 13958de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 14058de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 14158de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 14258de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 14358de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 14458de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 14558de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 14658de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 14758de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1480af41d30Smatthias.ringwald } 149808a48abSmatthias.ringwald 1506218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 1516218e6f1Smatthias.ringwald // track credits 1526218e6f1Smatthias.ringwald channel->packets_granted += credits; 1537b5fbe1fSmatthias.ringwald // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits); 1546218e6f1Smatthias.ringwald 1556218e6f1Smatthias.ringwald uint8_t event[5]; 1566218e6f1Smatthias.ringwald event[0] = L2CAP_EVENT_CREDITS; 1576218e6f1Smatthias.ringwald event[1] = sizeof(event) - 2; 1586218e6f1Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 1596218e6f1Smatthias.ringwald event[4] = credits; 1606218e6f1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1616218e6f1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1626218e6f1Smatthias.ringwald } 1636218e6f1Smatthias.ringwald 164808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){ 165808a48abSmatthias.ringwald new_credits_blocked = blocked; 166808a48abSmatthias.ringwald } 167808a48abSmatthias.ringwald 16840d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){ 169808a48abSmatthias.ringwald 170808a48abSmatthias.ringwald if (new_credits_blocked) return; // we're told not to. used by daemon 171808a48abSmatthias.ringwald 1728d371091Smatthias.ringwald linked_item_t *it; 1738d371091Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1748d371091Smatthias.ringwald if (!hci_number_free_acl_slots()) return; 1758d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 1760e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 1774c744e21Smatthias.ringwald if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 1788d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 1798d371091Smatthias.ringwald } 1808d371091Smatthias.ringwald } 1818d371091Smatthias.ringwald } 1828d371091Smatthias.ringwald 183b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 184f62db1e3Smatthias.ringwald linked_item_t *it; 185f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1868d371091Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 187b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 188f62db1e3Smatthias.ringwald return channel; 189f62db1e3Smatthias.ringwald } 190f62db1e3Smatthias.ringwald } 191f62db1e3Smatthias.ringwald return NULL; 192f62db1e3Smatthias.ringwald } 193f62db1e3Smatthias.ringwald 1946b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 1956b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 1966b1fde37Smatthias.ringwald if (!channel) return 0; 1976b1fde37Smatthias.ringwald if (!channel->packets_granted) return 0; 1986b1fde37Smatthias.ringwald return hci_can_send_packet_now(HCI_ACL_DATA_PACKET); 1996b1fde37Smatthias.ringwald } 2006b1fde37Smatthias.ringwald 20196cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 20296cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 20396cbd662Smatthias.ringwald if (channel) { 20496cbd662Smatthias.ringwald return channel->remote_mtu; 20596cbd662Smatthias.ringwald } 20696cbd662Smatthias.ringwald return 0; 20796cbd662Smatthias.ringwald } 20896cbd662Smatthias.ringwald 20958de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 210b1d43497Smatthias.ringwald 211b1d43497Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 212b1d43497Smatthias.ringwald log_info("l2cap_send_signaling_packet, cannot send\n"); 213b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 214b1d43497Smatthias.ringwald } 215b1d43497Smatthias.ringwald 2167b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet type %u\n", cmd); 217b1d43497Smatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 21858de5610Smatthias.ringwald va_list argptr; 21958de5610Smatthias.ringwald va_start(argptr, identifier); 220816c0598Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr); 22158de5610Smatthias.ringwald va_end(argptr); 2227b5fbe1fSmatthias.ringwald // log_info("l2cap_send_signaling_packet con %u!\n", handle); 223816c0598Smatthias.ringwald return hci_send_acl_packet(acl_buffer, len); 22458de5610Smatthias.ringwald } 22558de5610Smatthias.ringwald 226b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 227b1d43497Smatthias.ringwald return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 228b1d43497Smatthias.ringwald } 2296218e6f1Smatthias.ringwald 230b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 231b1d43497Smatthias.ringwald 232b1d43497Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 233b1d43497Smatthias.ringwald log_info("l2cap_send_internal cid %u, cannot send\n", local_cid); 234808a48abSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 2358ea03fa5Smatthias.ringwald } 2366218e6f1Smatthias.ringwald 23758de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 238b1d43497Smatthias.ringwald if (!channel) { 239b1d43497Smatthias.ringwald log_error("l2cap_send_internal no channel for cid %u\n", local_cid); 240b1d43497Smatthias.ringwald return -1; // TODO: define error 2416218e6f1Smatthias.ringwald } 2426218e6f1Smatthias.ringwald 243b1d43497Smatthias.ringwald if (channel->packets_granted == 0){ 244b1d43497Smatthias.ringwald log_error("l2cap_send_internal cid %u, no credits!\n", local_cid); 245b1d43497Smatthias.ringwald return -1; // TODO: define error 246b1d43497Smatthias.ringwald } 247b1d43497Smatthias.ringwald 248b1d43497Smatthias.ringwald --channel->packets_granted; 249b1d43497Smatthias.ringwald 250b1d43497Smatthias.ringwald log_debug("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n", 251b1d43497Smatthias.ringwald local_cid, channel->handle, channel->packets_granted); 252b1d43497Smatthias.ringwald 253b1d43497Smatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 254b1d43497Smatthias.ringwald 25558de5610Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 25658de5610Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 25758de5610Smatthias.ringwald // 2 - ACL length 25858de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 25958de5610Smatthias.ringwald // 4 - L2CAP packet length 26058de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 26158de5610Smatthias.ringwald // 6 - L2CAP channel DEST 26258de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 26358de5610Smatthias.ringwald // send 264b1d43497Smatthias.ringwald int err = hci_send_acl_packet(acl_buffer, len+8); 26591b99603Smatthias.ringwald 26691b99603Smatthias.ringwald l2cap_hand_out_credits(); 26791b99603Smatthias.ringwald 2686218e6f1Smatthias.ringwald return err; 26958de5610Smatthias.ringwald } 27058de5610Smatthias.ringwald 271b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 272b1d43497Smatthias.ringwald 273b1d43497Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 274b1d43497Smatthias.ringwald log_info("l2cap_send_internal cid %u, cannot send\n", local_cid); 275b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 276b1d43497Smatthias.ringwald } 277b1d43497Smatthias.ringwald 278b1d43497Smatthias.ringwald uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 279b1d43497Smatthias.ringwald 280b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 281b1d43497Smatthias.ringwald 282b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 283b1d43497Smatthias.ringwald } 284b1d43497Smatthias.ringwald 285b1d43497Smatthias.ringwald 2868158c421Smatthias.ringwald // MARK: L2CAP_RUN 2872cd0be45Smatthias.ringwald // process outstanding signaling tasks 2882cd0be45Smatthias.ringwald void l2cap_run(void){ 2892b83fb7dSmatthias.ringwald 2902b83fb7dSmatthias.ringwald // check pending signaling responses 2912b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 2922b83fb7dSmatthias.ringwald 29302b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 2942b83fb7dSmatthias.ringwald 2952b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 2962b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 2972b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 2982b360848Smatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST 2992b83fb7dSmatthias.ringwald 3002b83fb7dSmatthias.ringwald switch (signaling_responses[0].code){ 3012b360848Smatthias.ringwald case CONNECTION_REQUEST: 3022b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 3032b360848Smatthias.ringwald break; 3042b83fb7dSmatthias.ringwald case ECHO_REQUEST: 3052b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 3062b83fb7dSmatthias.ringwald break; 3072b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 3082b83fb7dSmatthias.ringwald if (infoType == 2) { 3092b83fb7dSmatthias.ringwald uint32_t features = 0; 3102b83fb7dSmatthias.ringwald // extended features request supported, however no features present 3112b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 3122b83fb7dSmatthias.ringwald } else { 3132b83fb7dSmatthias.ringwald // all other types are not supported 3142b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 3152b83fb7dSmatthias.ringwald } 3162b83fb7dSmatthias.ringwald break; 3172b83fb7dSmatthias.ringwald default: 3182b83fb7dSmatthias.ringwald // should not happen 3192b83fb7dSmatthias.ringwald break; 3202b83fb7dSmatthias.ringwald } 3212b83fb7dSmatthias.ringwald 3222b83fb7dSmatthias.ringwald // remove first item 3232b83fb7dSmatthias.ringwald signaling_responses_pending--; 3242b83fb7dSmatthias.ringwald int i; 3252b83fb7dSmatthias.ringwald for (i=0; i < signaling_responses_pending; i++){ 326*76115249S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 3272b83fb7dSmatthias.ringwald } 3282b83fb7dSmatthias.ringwald } 3292b83fb7dSmatthias.ringwald 330ae280e73Smatthias.ringwald uint8_t config_options[4]; 3312cd0be45Smatthias.ringwald linked_item_t *it; 3322cd0be45Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 3332cd0be45Smatthias.ringwald 33402b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 33502b22dc4Smatthias.ringwald if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 3362cd0be45Smatthias.ringwald 3372cd0be45Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 3386e6710ebSmatthias.ringwald 3397b5fbe1fSmatthias.ringwald // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 3406e6710ebSmatthias.ringwald 3412cd0be45Smatthias.ringwald switch (channel->state){ 3422cd0be45Smatthias.ringwald 34302b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 34464472d52Smatthias.ringwald // send connection request - set state first 34564472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 34602b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 3478f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 34802b22dc4Smatthias.ringwald break; 34902b22dc4Smatthias.ringwald 350e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 351b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0); 352e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 353e7ff783cSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 354d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 355e7ff783cSmatthias.ringwald break; 356e7ff783cSmatthias.ringwald 357552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 358fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 35973cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 3602a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 361552d92a1Smatthias.ringwald break; 362552d92a1Smatthias.ringwald 3636fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 3646fdcc387Smatthias.ringwald // success, start l2cap handshake 365b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 3666fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 3672a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 3686fdcc387Smatthias.ringwald break; 3696fdcc387Smatthias.ringwald 370fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 37173cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 37273cf2b3dSmatthias.ringwald channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP; 37373cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP; 374fa8473a4Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL); 375fa8473a4Smatthias.ringwald } 37673cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 37773cf2b3dSmatthias.ringwald channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 37873cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ; 379b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 380ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 381ae280e73Smatthias.ringwald config_options[1] = 2; // len param 382ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 383b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 384fa8473a4Smatthias.ringwald } 385fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 386552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 387552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 388552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 389fa8473a4Smatthias.ringwald } 390552d92a1Smatthias.ringwald break; 391552d92a1Smatthias.ringwald 392e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 393b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 394e7ff783cSmatthias.ringwald l2cap_finialize_channel_close(channel); 395e7ff783cSmatthias.ringwald break; 396e7ff783cSmatthias.ringwald 397e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 398b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 3992cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 4002a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 4012cd0be45Smatthias.ringwald break; 4022cd0be45Smatthias.ringwald default: 4032cd0be45Smatthias.ringwald break; 4042cd0be45Smatthias.ringwald } 4052cd0be45Smatthias.ringwald } 4062cd0be45Smatthias.ringwald } 4072cd0be45Smatthias.ringwald 4084aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 409816c0598Smatthias.ringwald return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE; 410fa8c92f6Smatthias.ringwald } 411fa8c92f6Smatthias.ringwald 4121e6aba47Smatthias.ringwald // open outgoing L2CAP channel 41315470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, 41415470d27Smatthias.ringwald bd_addr_t address, uint16_t psm, uint16_t mtu){ 4151e6aba47Smatthias.ringwald 4161e6aba47Smatthias.ringwald // alloc structure 417d3a9df87Smatthias.ringwald l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 4182b360848Smatthias.ringwald if (!chan) { 4192b360848Smatthias.ringwald // emit error event 4202b360848Smatthias.ringwald l2cap_channel_t dummy_channel; 4212b360848Smatthias.ringwald BD_ADDR_COPY(dummy_channel.address, address); 4222b360848Smatthias.ringwald dummy_channel.psm = psm; 4232b360848Smatthias.ringwald l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 4242b360848Smatthias.ringwald return; 4252b360848Smatthias.ringwald } 4261d279b20Smatthias.ringwald // limit local mtu to max acl packet length 4272985cb84Smatthias.ringwald if (mtu > l2cap_max_mtu()) { 4282985cb84Smatthias.ringwald mtu = l2cap_max_mtu(); 4299775e25bSmatthias.ringwald } 4309775e25bSmatthias.ringwald 4311e6aba47Smatthias.ringwald // fill in 4321e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 4331e6aba47Smatthias.ringwald chan->psm = psm; 4341e6aba47Smatthias.ringwald chan->handle = 0; 4351e6aba47Smatthias.ringwald chan->connection = connection; 4366b296a27Smatthias.ringwald chan->packet_handler = packet_handler; 4372784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 43815470d27Smatthias.ringwald chan->local_mtu = mtu; 4396218e6f1Smatthias.ringwald chan->packets_granted = 0; 4406218e6f1Smatthias.ringwald 4411e6aba47Smatthias.ringwald // set initial state 44202b22dc4Smatthias.ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 44373cf2b3dSmatthias.ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 444b1988dceSmatthias.ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 445b1988dceSmatthias.ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 4461e6aba47Smatthias.ringwald 4471e6aba47Smatthias.ringwald // add to connections list 4481e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 4491e6aba47Smatthias.ringwald 45002b22dc4Smatthias.ringwald l2cap_run(); 45143625864Smatthias.ringwald } 45243625864Smatthias.ringwald 453b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 454b35f641cSmatthias.ringwald // find channel for local_cid 455b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 456f62db1e3Smatthias.ringwald if (channel) { 457e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 458f62db1e3Smatthias.ringwald } 4592cd0be45Smatthias.ringwald // process 4602cd0be45Smatthias.ringwald l2cap_run(); 46143625864Smatthias.ringwald } 4621e6aba47Smatthias.ringwald 463afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 46415ec09bbSmatthias.ringwald linked_item_t *it = (linked_item_t *) &l2cap_channels; 46515ec09bbSmatthias.ringwald while (it->next){ 46615ec09bbSmatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 467b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 46802b22dc4Smatthias.ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 469afde0c52Smatthias.ringwald // failure, forward error code 470afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 471afde0c52Smatthias.ringwald // discard channel 47215ec09bbSmatthias.ringwald it->next = it->next->next; 473d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 474afde0c52Smatthias.ringwald } 47515ec09bbSmatthias.ringwald } else { 47615ec09bbSmatthias.ringwald it = it->next; 477afde0c52Smatthias.ringwald } 478afde0c52Smatthias.ringwald } 479afde0c52Smatthias.ringwald } 480afde0c52Smatthias.ringwald 481afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 482afde0c52Smatthias.ringwald linked_item_t *it; 483afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 484afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 485afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 48602b22dc4Smatthias.ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 487b448a0e7Smatthias.ringwald // success, start l2cap handshake 4886fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 489afde0c52Smatthias.ringwald channel->handle = handle; 490b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 491afde0c52Smatthias.ringwald } 492afde0c52Smatthias.ringwald } 493afde0c52Smatthias.ringwald } 4946fdcc387Smatthias.ringwald // process 4956fdcc387Smatthias.ringwald l2cap_run(); 496afde0c52Smatthias.ringwald } 497b448a0e7Smatthias.ringwald 498afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 499afde0c52Smatthias.ringwald 500afde0c52Smatthias.ringwald bd_addr_t address; 501afde0c52Smatthias.ringwald hci_con_handle_t handle; 5026e6710ebSmatthias.ringwald l2cap_channel_t * channel; 50336944dffSmatthias.ringwald linked_item_t *it; 5042d00edd4Smatthias.ringwald int hci_con_used; 505afde0c52Smatthias.ringwald 506afde0c52Smatthias.ringwald switch(packet[0]){ 507afde0c52Smatthias.ringwald 508afde0c52Smatthias.ringwald // handle connection complete events 509afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 510afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 511afde0c52Smatthias.ringwald if (packet[2] == 0){ 512afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 513afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 514afde0c52Smatthias.ringwald } else { 515afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 516afde0c52Smatthias.ringwald } 517afde0c52Smatthias.ringwald break; 518afde0c52Smatthias.ringwald 519afde0c52Smatthias.ringwald // handle successful create connection cancel command 520afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 521afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 522afde0c52Smatthias.ringwald if (packet[5] == 0){ 523afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 524afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 525afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 52603cfbabcSmatthias.ringwald } 5271e6aba47Smatthias.ringwald } 52839d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 52939d59809Smatthias.ringwald break; 53039d59809Smatthias.ringwald 53139d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 53239d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 533afde0c52Smatthias.ringwald break; 53427a923d0Smatthias.ringwald 5351e6aba47Smatthias.ringwald // handle disconnection complete events 536afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 53727a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 538afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 53915ec09bbSmatthias.ringwald it = (linked_item_t *) &l2cap_channels; 54015ec09bbSmatthias.ringwald while (it->next){ 54139ce35a7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 54227a923d0Smatthias.ringwald if ( channel->handle == handle ){ 54315ec09bbSmatthias.ringwald // update prev item before free'ing next element - don't call l2cap_finalize_channel_close 5442060cf14Smatthias.ringwald it->next = it->next->next; 54515ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 546d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 54715ec09bbSmatthias.ringwald } else { 54815ec09bbSmatthias.ringwald it = it->next; 54927a923d0Smatthias.ringwald } 55027a923d0Smatthias.ringwald } 551afde0c52Smatthias.ringwald break; 552fcadd0caSmatthias.ringwald 5536218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 55402b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 5558d371091Smatthias.ringwald l2cap_hand_out_credits(); 5566218e6f1Smatthias.ringwald break; 5576218e6f1Smatthias.ringwald 558ee091cf1Smatthias.ringwald // HCI Connection Timeouts 559afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 56036944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 56180ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 5622d00edd4Smatthias.ringwald hci_con_used = 0; 563ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 564ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 565ee091cf1Smatthias.ringwald if (channel->handle == handle) { 5662d00edd4Smatthias.ringwald hci_con_used = 1; 567ee091cf1Smatthias.ringwald } 568ee091cf1Smatthias.ringwald } 5692d00edd4Smatthias.ringwald if (hci_con_used) break; 57032ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 5719edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 572afde0c52Smatthias.ringwald break; 573ee091cf1Smatthias.ringwald 5746e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 5756e6710ebSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 5766e6710ebSmatthias.ringwald channel = (l2cap_channel_t *) it; 5776e6710ebSmatthias.ringwald if (channel->packet_handler) { 5786e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 5796e6710ebSmatthias.ringwald } 5806e6710ebSmatthias.ringwald } 5816e6710ebSmatthias.ringwald break; 5826e6710ebSmatthias.ringwald 583afde0c52Smatthias.ringwald default: 584afde0c52Smatthias.ringwald break; 585afde0c52Smatthias.ringwald } 586afde0c52Smatthias.ringwald 587afde0c52Smatthias.ringwald // pass on 588b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 5891e6aba47Smatthias.ringwald } 5901e6aba47Smatthias.ringwald 591afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 592b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 593e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 594e7ff783cSmatthias.ringwald l2cap_run(); 59584836b65Smatthias.ringwald } 59684836b65Smatthias.ringwald 5972b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 5982b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 5992b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 6002b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 6012b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 6022b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 6032b360848Smatthias.ringwald signaling_responses_pending++; 6042b360848Smatthias.ringwald l2cap_run(); 6052b360848Smatthias.ringwald } 6062b360848Smatthias.ringwald } 6072b360848Smatthias.ringwald 608b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 609645658c9Smatthias.ringwald 6107b5fbe1fSmatthias.ringwald // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 611645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 612645658c9Smatthias.ringwald if (!service) { 613645658c9Smatthias.ringwald // 0x0002 PSM not supported 6142b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 615645658c9Smatthias.ringwald return; 616645658c9Smatthias.ringwald } 617645658c9Smatthias.ringwald 618645658c9Smatthias.ringwald hci_connection_t * hci_connection = connection_for_handle( handle ); 619645658c9Smatthias.ringwald if (!hci_connection) { 6202b360848Smatthias.ringwald // 6217d67539fSmatthias.ringwald log_error("no hci_connection for handle %u\n", handle); 622645658c9Smatthias.ringwald return; 623645658c9Smatthias.ringwald } 624645658c9Smatthias.ringwald // alloc structure 6257b5fbe1fSmatthias.ringwald // log_info("l2cap_handle_connection_request register channel\n"); 626d3a9df87Smatthias.ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 6272b360848Smatthias.ringwald if (!channel){ 6282b360848Smatthias.ringwald // 0x0004 No resources available 6292b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 6302b360848Smatthias.ringwald return; 6312b360848Smatthias.ringwald } 632645658c9Smatthias.ringwald 633645658c9Smatthias.ringwald // fill in 634169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 635169f8b28Smatthias.ringwald channel->psm = psm; 636169f8b28Smatthias.ringwald channel->handle = handle; 637169f8b28Smatthias.ringwald channel->connection = service->connection; 638f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 639b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 640b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 641fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 6420a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 643761b0451Smatthias.ringwald channel->packets_granted = 0; 644b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 645645658c9Smatthias.ringwald 6461d279b20Smatthias.ringwald // limit local mtu to max acl packet length 6472985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 6482985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 6499775e25bSmatthias.ringwald } 6509775e25bSmatthias.ringwald 651645658c9Smatthias.ringwald // set initial state 652e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 65373cf2b3dSmatthias.ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 654e405ae81Smatthias.ringwald 655645658c9Smatthias.ringwald // add to connections list 656169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 657645658c9Smatthias.ringwald 658e405ae81Smatthias.ringwald // emit incoming connection request 659e405ae81Smatthias.ringwald l2cap_emit_connection_request(channel); 660e405ae81Smatthias.ringwald } 661645658c9Smatthias.ringwald 662b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 663b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 664e405ae81Smatthias.ringwald if (!channel) { 6657d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 666e405ae81Smatthias.ringwald return; 667e405ae81Smatthias.ringwald } 668e405ae81Smatthias.ringwald 669552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 670e405ae81Smatthias.ringwald 671552d92a1Smatthias.ringwald // process 672552d92a1Smatthias.ringwald l2cap_run(); 673e405ae81Smatthias.ringwald } 674645658c9Smatthias.ringwald 675b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 676b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 677e405ae81Smatthias.ringwald if (!channel) { 6787d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 679e405ae81Smatthias.ringwald return; 680e405ae81Smatthias.ringwald } 681e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 682e7ff783cSmatthias.ringwald channel->reason = reason; 683e7ff783cSmatthias.ringwald l2cap_run(); 684645658c9Smatthias.ringwald } 685645658c9Smatthias.ringwald 6862784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 687b1988dceSmatthias.ringwald 688b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 689b1988dceSmatthias.ringwald 6902784b77dSmatthias.ringwald // accept the other's configuration options 6913de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 6923de7c0caSmatthias.ringwald uint16_t pos = 8; 6933de7c0caSmatthias.ringwald while (pos < end_pos){ 6941dc511deSmatthias.ringwald uint8_t type = command[pos++]; 6951dc511deSmatthias.ringwald uint8_t length = command[pos++]; 6961dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 6971dc511deSmatthias.ringwald if ((type & 0x7f) == 1 && length == 2){ 6981dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 6997b5fbe1fSmatthias.ringwald // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 7001dc511deSmatthias.ringwald } 7011dc511deSmatthias.ringwald pos += length; 7021dc511deSmatthias.ringwald } 7032784b77dSmatthias.ringwald } 7042784b77dSmatthias.ringwald 705fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 7067b5fbe1fSmatthias.ringwald // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var); 70773cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 70873cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 709fa8473a4Smatthias.ringwald return 1; 710fa8473a4Smatthias.ringwald } 711fa8473a4Smatthias.ringwald 712fa8473a4Smatthias.ringwald 71300d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 7141e6aba47Smatthias.ringwald 71500d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 71600d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 71738e5900eSmatthias.ringwald uint16_t result = 0; 7181e6aba47Smatthias.ringwald 7197b5fbe1fSmatthias.ringwald log_info("signaling handler code %u, state %u\n", code, channel->state); 720b35f641cSmatthias.ringwald 7219a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 7229a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 7239a011532Smatthias.ringwald switch (channel->state){ 724fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 7259a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 7262b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 7279a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 7289a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 7299a011532Smatthias.ringwald break; 7309a011532Smatthias.ringwald 7319a011532Smatthias.ringwald default: 7329a011532Smatthias.ringwald // ignore in other states 7339a011532Smatthias.ringwald break; 7349a011532Smatthias.ringwald } 7359a011532Smatthias.ringwald return; 7369a011532Smatthias.ringwald } 7379a011532Smatthias.ringwald 7381e6aba47Smatthias.ringwald switch (channel->state) { 7391e6aba47Smatthias.ringwald 7401e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 7411e6aba47Smatthias.ringwald switch (code){ 7421e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 74300d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 74438e5900eSmatthias.ringwald switch (result) { 74538e5900eSmatthias.ringwald case 0: 746169f8b28Smatthias.ringwald // successful connection 74700d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 748fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 74973cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 75038e5900eSmatthias.ringwald break; 75138e5900eSmatthias.ringwald case 1: 75238e5900eSmatthias.ringwald // connection pending. get some coffee 75338e5900eSmatthias.ringwald break; 75438e5900eSmatthias.ringwald default: 755eb920dbeSmatthias.ringwald // channel closed 756eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 757eb920dbeSmatthias.ringwald 758f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 75938e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 760eb920dbeSmatthias.ringwald 761eb920dbeSmatthias.ringwald // drop link key if security block 762eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 763eb920dbeSmatthias.ringwald hci_drop_link_key_for_bd_addr(&channel->address); 764eb920dbeSmatthias.ringwald } 765eb920dbeSmatthias.ringwald 766eb920dbeSmatthias.ringwald // discard channel 767eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 768d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 76938e5900eSmatthias.ringwald break; 7701e6aba47Smatthias.ringwald } 7711e6aba47Smatthias.ringwald break; 77238e5900eSmatthias.ringwald 77338e5900eSmatthias.ringwald default: 7741e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 77538e5900eSmatthias.ringwald break; 7761e6aba47Smatthias.ringwald } 7771e6aba47Smatthias.ringwald break; 7781e6aba47Smatthias.ringwald 779fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 780ae280e73Smatthias.ringwald switch (code) { 781ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 78273cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ; 78373cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP; 784ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 785ae280e73Smatthias.ringwald break; 7861e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 78773cf2b3dSmatthias.ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP; 7885a67bd4aSmatthias.ringwald break; 7895a67bd4aSmatthias.ringwald default: 7905a67bd4aSmatthias.ringwald break; 7911e6aba47Smatthias.ringwald } 792fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 793fa8473a4Smatthias.ringwald // for open: 7945a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 795fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 7966218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 797c8e4258aSmatthias.ringwald } 798c8e4258aSmatthias.ringwald break; 799f62db1e3Smatthias.ringwald 800f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 801f62db1e3Smatthias.ringwald switch (code) { 802f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 80327a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 80427a923d0Smatthias.ringwald break; 8055a67bd4aSmatthias.ringwald default: 8065a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 8075a67bd4aSmatthias.ringwald break; 80827a923d0Smatthias.ringwald } 80927a923d0Smatthias.ringwald break; 81084836b65Smatthias.ringwald 81184836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 81284836b65Smatthias.ringwald // @TODO handle incoming requests 81384836b65Smatthias.ringwald break; 81484836b65Smatthias.ringwald 81584836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 81684836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 81784836b65Smatthias.ringwald break; 81810642e45Smatthias.ringwald default: 81910642e45Smatthias.ringwald break; 82027a923d0Smatthias.ringwald } 8217b5fbe1fSmatthias.ringwald // log_info("new state %u\n", channel->state); 82227a923d0Smatthias.ringwald } 82327a923d0Smatthias.ringwald 82400d93d79Smatthias.ringwald 82500d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 82600d93d79Smatthias.ringwald 82700d93d79Smatthias.ringwald // get code, signalind identifier and command len 82800d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 82900d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 83000d93d79Smatthias.ringwald 83100d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 83200d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 83300d93d79Smatthias.ringwald return; 83400d93d79Smatthias.ringwald } 83500d93d79Smatthias.ringwald 83600d93d79Smatthias.ringwald // general commands without an assigned channel 83700d93d79Smatthias.ringwald switch(code) { 83800d93d79Smatthias.ringwald 83900d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 84000d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 84100d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 84200d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 8432b83fb7dSmatthias.ringwald return; 84400d93d79Smatthias.ringwald } 84500d93d79Smatthias.ringwald 8462b360848Smatthias.ringwald case ECHO_REQUEST: 8472b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 8482b83fb7dSmatthias.ringwald return; 84900d93d79Smatthias.ringwald 85000d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 85100d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 8522b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 8532b83fb7dSmatthias.ringwald return; 85400d93d79Smatthias.ringwald } 85500d93d79Smatthias.ringwald 85600d93d79Smatthias.ringwald default: 85700d93d79Smatthias.ringwald break; 85800d93d79Smatthias.ringwald } 85900d93d79Smatthias.ringwald 86000d93d79Smatthias.ringwald 86100d93d79Smatthias.ringwald // Get potential destination CID 86200d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 86300d93d79Smatthias.ringwald 86400d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 86500d93d79Smatthias.ringwald linked_item_t *it; 86600d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 86700d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 86800d93d79Smatthias.ringwald if (channel->handle == handle) { 86900d93d79Smatthias.ringwald if (code & 1) { 870b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 871b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 87200d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 8734e32727eSmatthias.ringwald break; 87400d93d79Smatthias.ringwald } 87500d93d79Smatthias.ringwald } else { 876b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 87700d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 87800d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 8794e32727eSmatthias.ringwald break; 88000d93d79Smatthias.ringwald } 88100d93d79Smatthias.ringwald } 88200d93d79Smatthias.ringwald } 88300d93d79Smatthias.ringwald } 88400d93d79Smatthias.ringwald } 88500d93d79Smatthias.ringwald 88600d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 88700d93d79Smatthias.ringwald 88800d93d79Smatthias.ringwald // Get Channel ID 88900d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 89000d93d79Smatthias.ringwald 89100d93d79Smatthias.ringwald // Signaling Packet? 89200d93d79Smatthias.ringwald if (channel_id == 1) { 89300d93d79Smatthias.ringwald 89400d93d79Smatthias.ringwald // Get Connection 89500d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 89600d93d79Smatthias.ringwald 89700d93d79Smatthias.ringwald uint16_t command_offset = 8; 89800d93d79Smatthias.ringwald while (command_offset < size) { 89900d93d79Smatthias.ringwald 90000d93d79Smatthias.ringwald // handle signaling commands 90100d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 90200d93d79Smatthias.ringwald 90300d93d79Smatthias.ringwald // increment command_offset 90400d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 90500d93d79Smatthias.ringwald } 90664472d52Smatthias.ringwald 90764472d52Smatthias.ringwald l2cap_run(); 90864472d52Smatthias.ringwald 90900d93d79Smatthias.ringwald return; 91000d93d79Smatthias.ringwald } 91100d93d79Smatthias.ringwald 91200d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 91300d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 91400d93d79Smatthias.ringwald if (channel) { 91558de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 91600d93d79Smatthias.ringwald } 91700d93d79Smatthias.ringwald } 91800d93d79Smatthias.ringwald 9192718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 9202718e2e7Smatthias.ringwald switch (packet_type) { 9212718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 9222718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 9232718e2e7Smatthias.ringwald break; 9242718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 9252718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 9262718e2e7Smatthias.ringwald break; 9272718e2e7Smatthias.ringwald default: 9282718e2e7Smatthias.ringwald break; 9292718e2e7Smatthias.ringwald } 9302718e2e7Smatthias.ringwald } 93100d93d79Smatthias.ringwald 93215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 93327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 934f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 935f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 936f62db1e3Smatthias.ringwald // discard channel 937f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 938d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 939c8e4258aSmatthias.ringwald } 9401e6aba47Smatthias.ringwald 9419d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 942c52bf64dSmatthias.ringwald linked_item_t *it; 9439d9bbc01Smatthias.ringwald 9449d9bbc01Smatthias.ringwald // close open channels 9459d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 9469d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 9479d9bbc01Smatthias.ringwald if ( service->psm == psm){ 9489d9bbc01Smatthias.ringwald return service; 9499d9bbc01Smatthias.ringwald }; 9509d9bbc01Smatthias.ringwald } 9519d9bbc01Smatthias.ringwald return NULL; 9529d9bbc01Smatthias.ringwald } 9539d9bbc01Smatthias.ringwald 95436944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 9554bb582b6Smatthias.ringwald // check for alread registered psm 9564bb582b6Smatthias.ringwald // TODO: emit error event 9579d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 958277abc2cSmatthias.ringwald if (service) { 959277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: PSM %u already registered\n", psm); 960277abc2cSmatthias.ringwald return; 961277abc2cSmatthias.ringwald } 9629d9bbc01Smatthias.ringwald 9634bb582b6Smatthias.ringwald // alloc structure 9644bb582b6Smatthias.ringwald // TODO: emit error event 965d3a9df87Smatthias.ringwald service = btstack_memory_l2cap_service_get(); 966277abc2cSmatthias.ringwald if (!service) { 967277abc2cSmatthias.ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n"); 968277abc2cSmatthias.ringwald return; 969277abc2cSmatthias.ringwald } 9709d9bbc01Smatthias.ringwald 9719d9bbc01Smatthias.ringwald // fill in 9729d9bbc01Smatthias.ringwald service->psm = psm; 9739d9bbc01Smatthias.ringwald service->mtu = mtu; 9749d9bbc01Smatthias.ringwald service->connection = connection; 975d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 9769d9bbc01Smatthias.ringwald 9779d9bbc01Smatthias.ringwald // add to services list 9789d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 9799d9bbc01Smatthias.ringwald } 9809d9bbc01Smatthias.ringwald 98136944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 9829d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 983037d6e48Smatthias.ringwald if (!service) return; 9849d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 985d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 9869d9bbc01Smatthias.ringwald } 9879d9bbc01Smatthias.ringwald 9889d9bbc01Smatthias.ringwald // 98936944dffSmatthias.ringwald void l2cap_close_connection(void *connection){ 9909d9bbc01Smatthias.ringwald linked_item_t *it; 9919d9bbc01Smatthias.ringwald 9923a9e0a58Smatthias.ringwald // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks 993c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 994c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 995c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 996c52bf64dSmatthias.ringwald if (channel->connection == connection) { 997cb8eb7e6Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 998c52bf64dSmatthias.ringwald } 999c52bf64dSmatthias.ringwald } 10009d9bbc01Smatthias.ringwald 1001645658c9Smatthias.ringwald // unregister services 100269025de8Smatthias.ringwald it = (linked_item_t *) &l2cap_services; 100369025de8Smatthias.ringwald while (it->next) { 100469025de8Smatthias.ringwald l2cap_service_t * service = (l2cap_service_t *) it->next; 1005645658c9Smatthias.ringwald if (service->connection == connection){ 100669025de8Smatthias.ringwald it->next = it->next->next; 1007d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 10088149d2c7Smatthias.ringwald } else { 10098149d2c7Smatthias.ringwald it = it->next; 1010645658c9Smatthias.ringwald } 1011645658c9Smatthias.ringwald } 1012cb8eb7e6Smatthias.ringwald 1013cb8eb7e6Smatthias.ringwald // process 1014cb8eb7e6Smatthias.ringwald l2cap_run(); 1015c52bf64dSmatthias.ringwald } 1016c52bf64dSmatthias.ringwald