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" 4343625864Smatthias.ringwald 4443625864Smatthias.ringwald #include <stdarg.h> 4543625864Smatthias.ringwald #include <string.h> 4643625864Smatthias.ringwald 4743625864Smatthias.ringwald #include <stdio.h> 4843625864Smatthias.ringwald 496f60b3f4Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets 506f60b3f4Smatthias.ringwald #define COMPLETE_L2CAP_HEADER 8 516f60b3f4Smatthias.ringwald 5200d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 5300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 5400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 5500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 5600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 5700d93d79Smatthias.ringwald 580d5a78dbSmatthias.ringwald static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size); 590d5a78dbSmatthias.ringwald static void null_data_handler(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size); 60fcadd0caSmatthias.ringwald 611e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL; 621e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL; 639d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL; 641e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL; 650d5a78dbSmatthias.ringwald static void (*event_packet_handler) (connection_t * connection, uint8_t *packet, uint16_t size) = null_event_handler; 660d5a78dbSmatthias.ringwald static void (*data_packet_handler) (connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size) = null_data_handler; 679edc8742Smatthias.ringwald static connection_t * capture_connection = NULL; 681e6aba47Smatthias.ringwald 69169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 70169f8b28Smatthias.ringwald 711e6aba47Smatthias.ringwald void l2cap_init(){ 721e6aba47Smatthias.ringwald sig_buffer = malloc( 48 ); 731e6aba47Smatthias.ringwald acl_buffer = malloc( 255 + 8 ); 74fcadd0caSmatthias.ringwald 75fcadd0caSmatthias.ringwald // 76fcadd0caSmatthias.ringwald // register callbacks with HCI 77fcadd0caSmatthias.ringwald // 78fcadd0caSmatthias.ringwald hci_register_event_packet_handler(&l2cap_event_handler); 79fcadd0caSmatthias.ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 80fcadd0caSmatthias.ringwald } 81fcadd0caSmatthias.ringwald 82fcadd0caSmatthias.ringwald 83fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 840d5a78dbSmatthias.ringwald static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size){ 85fcadd0caSmatthias.ringwald } 860d5a78dbSmatthias.ringwald static void null_data_handler(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size){ 87fcadd0caSmatthias.ringwald } 880d5a78dbSmatthias.ringwald void l2cap_register_event_packet_handler(void (*handler)(connection_t * connection, uint8_t *packet, uint16_t size)){ 89fcadd0caSmatthias.ringwald event_packet_handler = handler; 90fcadd0caSmatthias.ringwald } 910d5a78dbSmatthias.ringwald void l2cap_register_data_packet_handler (void (*handler)(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size)){ 92fcadd0caSmatthias.ringwald data_packet_handler = handler; 931e6aba47Smatthias.ringwald } 941e6aba47Smatthias.ringwald 9558de5610Smatthias.ringwald // notify client/protocol handler 9658de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 9758de5610Smatthias.ringwald if (channel->packet_handler) { 9858de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 9958de5610Smatthias.ringwald } else { 10058de5610Smatthias.ringwald switch (type){ 10158de5610Smatthias.ringwald case HCI_EVENT_PACKET: 10258de5610Smatthias.ringwald (*event_packet_handler)(channel->connection, data, size); 10358de5610Smatthias.ringwald break; 10458de5610Smatthias.ringwald case L2CAP_DATA_PACKET: 10558de5610Smatthias.ringwald (*data_packet_handler)(channel->connection, channel->local_cid, data, size); 10658de5610Smatthias.ringwald break; 10758de5610Smatthias.ringwald default: 10858de5610Smatthias.ringwald // ?? 10958de5610Smatthias.ringwald break; 11058de5610Smatthias.ringwald } 11158de5610Smatthias.ringwald } 11258de5610Smatthias.ringwald } 11358de5610Smatthias.ringwald 11458de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 11558de5610Smatthias.ringwald uint8_t event[17]; 11658de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 11758de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 11858de5610Smatthias.ringwald event[2] = status; 11958de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 12058de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 12158de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 12258de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 12358de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 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 } 1490af41d30Smatthias.ringwald 150b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 151f62db1e3Smatthias.ringwald linked_item_t *it; 152f62db1e3Smatthias.ringwald l2cap_channel_t * channel; 153f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 154f62db1e3Smatthias.ringwald channel = (l2cap_channel_t *) it; 155b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 156f62db1e3Smatthias.ringwald return channel; 157f62db1e3Smatthias.ringwald } 158f62db1e3Smatthias.ringwald } 159f62db1e3Smatthias.ringwald return NULL; 160f62db1e3Smatthias.ringwald } 161f62db1e3Smatthias.ringwald 16258de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 16358de5610Smatthias.ringwald // printf("l2cap_send_signaling_packet type %u\n", cmd); 16458de5610Smatthias.ringwald va_list argptr; 16558de5610Smatthias.ringwald va_start(argptr, identifier); 16658de5610Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 16758de5610Smatthias.ringwald va_end(argptr); 16858de5610Smatthias.ringwald return hci_send_acl_packet(sig_buffer, len); 16958de5610Smatthias.ringwald } 17058de5610Smatthias.ringwald 17158de5610Smatthias.ringwald void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 17258de5610Smatthias.ringwald // find channel for local_cid, construct l2cap packet and send 17358de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 17458de5610Smatthias.ringwald if (channel) { 17558de5610Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 17658de5610Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 17758de5610Smatthias.ringwald // 2 - ACL length 17858de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 17958de5610Smatthias.ringwald // 4 - L2CAP packet length 18058de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 18158de5610Smatthias.ringwald // 6 - L2CAP channel DEST 18258de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 18358de5610Smatthias.ringwald // 8 - data 18458de5610Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 18558de5610Smatthias.ringwald // send 18658de5610Smatthias.ringwald hci_send_acl_packet(acl_buffer, len+8); 18758de5610Smatthias.ringwald } 18858de5610Smatthias.ringwald } 18958de5610Smatthias.ringwald 1901e6aba47Smatthias.ringwald // open outgoing L2CAP channel 1911e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){ 1921e6aba47Smatthias.ringwald 1931e6aba47Smatthias.ringwald // alloc structure 1941e6aba47Smatthias.ringwald l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 1951e6aba47Smatthias.ringwald // TODO: emit error event 1961e6aba47Smatthias.ringwald if (!chan) return; 1971e6aba47Smatthias.ringwald 1981e6aba47Smatthias.ringwald // fill in 1991e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 2001e6aba47Smatthias.ringwald chan->psm = psm; 2011e6aba47Smatthias.ringwald chan->handle = 0; 2021e6aba47Smatthias.ringwald chan->connection = connection; 203b5a49eb1Smatthias.ringwald chan->packet_handler = NULL; 2041e6aba47Smatthias.ringwald 2051e6aba47Smatthias.ringwald // set initial state 2061e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_CLOSED; 2071e6aba47Smatthias.ringwald chan->sig_id = L2CAP_SIG_ID_INVALID; 2081e6aba47Smatthias.ringwald 2091e6aba47Smatthias.ringwald // add to connections list 2101e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 2111e6aba47Smatthias.ringwald 2121e6aba47Smatthias.ringwald // send connection request 2131e6aba47Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 2141e6aba47Smatthias.ringwald hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 21543625864Smatthias.ringwald } 21643625864Smatthias.ringwald 217b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 218b35f641cSmatthias.ringwald // find channel for local_cid 219b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 220f62db1e3Smatthias.ringwald if (channel) { 221f62db1e3Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 222b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 223f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 224f62db1e3Smatthias.ringwald } 22543625864Smatthias.ringwald } 2261e6aba47Smatthias.ringwald 227afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 2281e6aba47Smatthias.ringwald linked_item_t *it; 2291e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 230b448a0e7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 231b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 232b448a0e7Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 233afde0c52Smatthias.ringwald // failure, forward error code 234afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 235afde0c52Smatthias.ringwald // discard channel 236afde0c52Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 237afde0c52Smatthias.ringwald free (channel); 238afde0c52Smatthias.ringwald } 239afde0c52Smatthias.ringwald } 240afde0c52Smatthias.ringwald } 241afde0c52Smatthias.ringwald } 242afde0c52Smatthias.ringwald 243afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 244afde0c52Smatthias.ringwald linked_item_t *it; 245afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 246afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 247afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 248afde0c52Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 249b448a0e7Smatthias.ringwald // success, start l2cap handshake 250afde0c52Smatthias.ringwald channel->handle = handle; 251b448a0e7Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 252b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 253b448a0e7Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 254b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid); 255afde0c52Smatthias.ringwald } 256afde0c52Smatthias.ringwald } 257afde0c52Smatthias.ringwald } 258afde0c52Smatthias.ringwald } 259b448a0e7Smatthias.ringwald 260afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 261afde0c52Smatthias.ringwald 262afde0c52Smatthias.ringwald bd_addr_t address; 263afde0c52Smatthias.ringwald hci_con_handle_t handle; 264afde0c52Smatthias.ringwald 265afde0c52Smatthias.ringwald switch(packet[0]){ 266afde0c52Smatthias.ringwald 267afde0c52Smatthias.ringwald // handle connection complete events 268afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 269afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 270afde0c52Smatthias.ringwald if (packet[2] == 0){ 271afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 272afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 273afde0c52Smatthias.ringwald } else { 274afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 275afde0c52Smatthias.ringwald } 276afde0c52Smatthias.ringwald break; 277afde0c52Smatthias.ringwald 278afde0c52Smatthias.ringwald // handle successful create connection cancel command 279afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 280afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 281afde0c52Smatthias.ringwald if (packet[5] == 0){ 282afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 283afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 284afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 28503cfbabcSmatthias.ringwald } 2861e6aba47Smatthias.ringwald } 287afde0c52Smatthias.ringwald break; 28827a923d0Smatthias.ringwald 2891e6aba47Smatthias.ringwald // handle disconnection complete events 290afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 29127a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 292afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 29327a923d0Smatthias.ringwald linked_item_t *it; 29427a923d0Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 29527a923d0Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 29627a923d0Smatthias.ringwald if ( channel->handle == handle ){ 29727a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 29827a923d0Smatthias.ringwald } 29927a923d0Smatthias.ringwald } 300afde0c52Smatthias.ringwald break; 301fcadd0caSmatthias.ringwald 302ee091cf1Smatthias.ringwald // HCI Connection Timeouts 303afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 304afde0c52Smatthias.ringwald if (!capture_connection){ 305ee091cf1Smatthias.ringwald hci_con_handle_t handle = READ_BT_16(packet, 2); 306ee091cf1Smatthias.ringwald linked_item_t *it; 307ee091cf1Smatthias.ringwald l2cap_channel_t * channel; 308ee091cf1Smatthias.ringwald int used = 0; 309ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 310ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 311ee091cf1Smatthias.ringwald if (channel->handle == handle) { 312ee091cf1Smatthias.ringwald used = 1; 313ee091cf1Smatthias.ringwald } 314ee091cf1Smatthias.ringwald } 315ee091cf1Smatthias.ringwald if (!used) { 3169edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 317ee091cf1Smatthias.ringwald } 318ee091cf1Smatthias.ringwald } 319afde0c52Smatthias.ringwald break; 320ee091cf1Smatthias.ringwald 321afde0c52Smatthias.ringwald default: 322afde0c52Smatthias.ringwald break; 323afde0c52Smatthias.ringwald } 324afde0c52Smatthias.ringwald 325afde0c52Smatthias.ringwald // pass on 3260d5a78dbSmatthias.ringwald (*event_packet_handler)(NULL, packet, size); 3271e6aba47Smatthias.ringwald } 3281e6aba47Smatthias.ringwald 329afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 330b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->remote_cid, channel->local_cid); 33184836b65Smatthias.ringwald l2cap_finialize_channel_close(channel); 33284836b65Smatthias.ringwald } 33384836b65Smatthias.ringwald 334b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 335645658c9Smatthias.ringwald 336b35f641cSmatthias.ringwald // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 337645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 338645658c9Smatthias.ringwald if (!service) { 339645658c9Smatthias.ringwald // 0x0002 PSM not supported 340169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm); 341645658c9Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0); 342645658c9Smatthias.ringwald return; 343645658c9Smatthias.ringwald } 344645658c9Smatthias.ringwald 345645658c9Smatthias.ringwald hci_connection_t * hci_connection = connection_for_handle( handle ); 346645658c9Smatthias.ringwald if (!hci_connection) { 347169f8b28Smatthias.ringwald fprintf(stderr, "no hci_connection for handle %u\n", handle); 348645658c9Smatthias.ringwald // TODO: emit error 349645658c9Smatthias.ringwald return; 350645658c9Smatthias.ringwald } 351645658c9Smatthias.ringwald // alloc structure 352169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request register channel\n"); 353169f8b28Smatthias.ringwald l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t)); 354645658c9Smatthias.ringwald // TODO: emit error event 355169f8b28Smatthias.ringwald if (!channel) return; 356645658c9Smatthias.ringwald 357645658c9Smatthias.ringwald // fill in 358169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 359169f8b28Smatthias.ringwald channel->psm = psm; 360169f8b28Smatthias.ringwald channel->handle = handle; 361169f8b28Smatthias.ringwald channel->connection = service->connection; 362f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 363b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 364b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 365645658c9Smatthias.ringwald 366645658c9Smatthias.ringwald // set initial state 367e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 368e405ae81Smatthias.ringwald 369e405ae81Smatthias.ringwald // temp. store req sig id 370e405ae81Smatthias.ringwald channel->sig_id = sig_id; 371645658c9Smatthias.ringwald 372645658c9Smatthias.ringwald // add to connections list 373169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 374645658c9Smatthias.ringwald 375e405ae81Smatthias.ringwald // emit incoming connection request 376e405ae81Smatthias.ringwald l2cap_emit_connection_request(channel); 377e405ae81Smatthias.ringwald } 378645658c9Smatthias.ringwald 379b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 380b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 381e405ae81Smatthias.ringwald if (!channel) { 382b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 383e405ae81Smatthias.ringwald return; 384e405ae81Smatthias.ringwald } 385e405ae81Smatthias.ringwald 386e405ae81Smatthias.ringwald // accept connection 387b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0); 388e405ae81Smatthias.ringwald 389e405ae81Smatthias.ringwald // set real sig and state and start config 390e405ae81Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 391e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 392b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 393b35f641cSmatthias.ringwald 394b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 395e405ae81Smatthias.ringwald } 396645658c9Smatthias.ringwald 397b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 398b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 399e405ae81Smatthias.ringwald if (!channel) { 400b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid,reason); 401e405ae81Smatthias.ringwald return; 402e405ae81Smatthias.ringwald } 403e405ae81Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0); 404e405ae81Smatthias.ringwald 405e405ae81Smatthias.ringwald // discard channel 406e405ae81Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 407e405ae81Smatthias.ringwald free (channel); 408645658c9Smatthias.ringwald } 409645658c9Smatthias.ringwald 41000d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 4111e6aba47Smatthias.ringwald 41200d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 41300d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 41400d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 41538e5900eSmatthias.ringwald uint16_t result = 0; 4161e6aba47Smatthias.ringwald 417b35f641cSmatthias.ringwald // printf("signaling handler code %u\n", code); 418b35f641cSmatthias.ringwald 4191e6aba47Smatthias.ringwald switch (channel->state) { 4201e6aba47Smatthias.ringwald 4211e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 4221e6aba47Smatthias.ringwald switch (code){ 4231e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 42400d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 42538e5900eSmatthias.ringwald switch (result) { 42638e5900eSmatthias.ringwald case 0: 427169f8b28Smatthias.ringwald // successful connection 42800d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 4291e6aba47Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 430b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 4315a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 43238e5900eSmatthias.ringwald break; 43338e5900eSmatthias.ringwald case 1: 43438e5900eSmatthias.ringwald // connection pending. get some coffee 43538e5900eSmatthias.ringwald break; 43638e5900eSmatthias.ringwald default: 437f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 43838e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 43938e5900eSmatthias.ringwald break; 4401e6aba47Smatthias.ringwald } 4411e6aba47Smatthias.ringwald break; 44238e5900eSmatthias.ringwald 44384836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 44484836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 44584836b65Smatthias.ringwald break; 44684836b65Smatthias.ringwald 44738e5900eSmatthias.ringwald default: 4481e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 44938e5900eSmatthias.ringwald break; 4501e6aba47Smatthias.ringwald } 4511e6aba47Smatthias.ringwald break; 4521e6aba47Smatthias.ringwald 4535a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 4541e6aba47Smatthias.ringwald switch (code) { 4551e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 4561e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 4571e6aba47Smatthias.ringwald break; 4585a67bd4aSmatthias.ringwald case CONFIGURE_REQUEST: 4595a67bd4aSmatthias.ringwald // accept the other's configuration options 46000d93d79Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 4615a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 4625a67bd4aSmatthias.ringwald break; 46384836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 46484836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 46584836b65Smatthias.ringwald break; 4665a67bd4aSmatthias.ringwald default: 4675a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4685a67bd4aSmatthias.ringwald break; 4691e6aba47Smatthias.ringwald } 4701e6aba47Smatthias.ringwald break; 4711e6aba47Smatthias.ringwald 4721e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ: 4731e6aba47Smatthias.ringwald switch (code) { 4741e6aba47Smatthias.ringwald case CONFIGURE_REQUEST: 4751e6aba47Smatthias.ringwald // accept the other's configuration options 47600d93d79Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 4771e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 47803cfbabcSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 479c8e4258aSmatthias.ringwald break; 48084836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 48184836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 48284836b65Smatthias.ringwald break; 4835a67bd4aSmatthias.ringwald default: 4845a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4855a67bd4aSmatthias.ringwald break; 4865a67bd4aSmatthias.ringwald } 4875a67bd4aSmatthias.ringwald break; 4885a67bd4aSmatthias.ringwald 4895a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 4905a67bd4aSmatthias.ringwald switch (code) { 4915a67bd4aSmatthias.ringwald case CONFIGURE_RESPONSE: 4925a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 4935a67bd4aSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 4945a67bd4aSmatthias.ringwald break; 49584836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 49684836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 49784836b65Smatthias.ringwald break; 4985a67bd4aSmatthias.ringwald default: 4995a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 5005a67bd4aSmatthias.ringwald break; 501c8e4258aSmatthias.ringwald } 502c8e4258aSmatthias.ringwald break; 503f62db1e3Smatthias.ringwald 504f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 505f62db1e3Smatthias.ringwald switch (code) { 506f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 50727a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 50827a923d0Smatthias.ringwald break; 50984836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 51084836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 51184836b65Smatthias.ringwald break; 5125a67bd4aSmatthias.ringwald default: 5135a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 5145a67bd4aSmatthias.ringwald break; 51527a923d0Smatthias.ringwald } 51627a923d0Smatthias.ringwald break; 51784836b65Smatthias.ringwald 51884836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 51984836b65Smatthias.ringwald // @TODO handle incoming requests 52084836b65Smatthias.ringwald break; 52184836b65Smatthias.ringwald 52284836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 52384836b65Smatthias.ringwald switch (code) { 52484836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 52584836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 52684836b65Smatthias.ringwald break; 5275a67bd4aSmatthias.ringwald default: 52884836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 52984836b65Smatthias.ringwald break; 53084836b65Smatthias.ringwald } 5315a67bd4aSmatthias.ringwald break; 53227a923d0Smatthias.ringwald } 533b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 53427a923d0Smatthias.ringwald } 53527a923d0Smatthias.ringwald 53600d93d79Smatthias.ringwald 53700d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 53800d93d79Smatthias.ringwald 53900d93d79Smatthias.ringwald // get code, signalind identifier and command len 54000d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 54100d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 54200d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 54300d93d79Smatthias.ringwald 54400d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 54500d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 54600d93d79Smatthias.ringwald return; 54700d93d79Smatthias.ringwald } 54800d93d79Smatthias.ringwald 54900d93d79Smatthias.ringwald // general commands without an assigned channel 55000d93d79Smatthias.ringwald switch(code) { 55100d93d79Smatthias.ringwald 55200d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 55300d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 55400d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 55500d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 55600d93d79Smatthias.ringwald break; 55700d93d79Smatthias.ringwald } 55800d93d79Smatthias.ringwald 55900d93d79Smatthias.ringwald case ECHO_REQUEST: { 56000d93d79Smatthias.ringwald // send back packet 56100d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]); 56200d93d79Smatthias.ringwald break; 56300d93d79Smatthias.ringwald } 56400d93d79Smatthias.ringwald 56500d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 56600d93d79Smatthias.ringwald // we neither support connectionless L2CAP data nor support any flow control modes yet 56700d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 56800d93d79Smatthias.ringwald if (infoType == 2) { 56900d93d79Smatthias.ringwald uint32_t features = 0; 57000d93d79Smatthias.ringwald // extended features request supported, however no features present 57100d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 57200d93d79Smatthias.ringwald } else { 57300d93d79Smatthias.ringwald // all other types are not supported 57400d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 57500d93d79Smatthias.ringwald } 57600d93d79Smatthias.ringwald break;; 57700d93d79Smatthias.ringwald } 57800d93d79Smatthias.ringwald 57900d93d79Smatthias.ringwald default: 58000d93d79Smatthias.ringwald break; 58100d93d79Smatthias.ringwald } 58200d93d79Smatthias.ringwald 58300d93d79Smatthias.ringwald 58400d93d79Smatthias.ringwald // Get potential destination CID 58500d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 58600d93d79Smatthias.ringwald 58700d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 58800d93d79Smatthias.ringwald linked_item_t *it; 58900d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 59000d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 59100d93d79Smatthias.ringwald if (channel->handle == handle) { 59200d93d79Smatthias.ringwald if (code & 1) { 59300d93d79Smatthias.ringwald // match odd commands by previous signaling identifier 59400d93d79Smatthias.ringwald if (channel->sig_id == sig_id) { 59500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 59600d93d79Smatthias.ringwald } 59700d93d79Smatthias.ringwald } else { 59800d93d79Smatthias.ringwald // match even commands by local channel id 59900d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 60000d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 60100d93d79Smatthias.ringwald } 60200d93d79Smatthias.ringwald } 60300d93d79Smatthias.ringwald } 60400d93d79Smatthias.ringwald } 60500d93d79Smatthias.ringwald } 60600d93d79Smatthias.ringwald 60700d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 60800d93d79Smatthias.ringwald 60900d93d79Smatthias.ringwald // Capturing? 61000d93d79Smatthias.ringwald if (capture_connection) { 61100d93d79Smatthias.ringwald (*data_packet_handler)(capture_connection, 0, packet, size); 61200d93d79Smatthias.ringwald return; 61300d93d79Smatthias.ringwald } 61400d93d79Smatthias.ringwald 61500d93d79Smatthias.ringwald // Get Channel ID 61600d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 61700d93d79Smatthias.ringwald 61800d93d79Smatthias.ringwald // Signaling Packet? 61900d93d79Smatthias.ringwald if (channel_id == 1) { 62000d93d79Smatthias.ringwald 62100d93d79Smatthias.ringwald // Get Connection 62200d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 62300d93d79Smatthias.ringwald 62400d93d79Smatthias.ringwald uint16_t command_offset = 8; 62500d93d79Smatthias.ringwald while (command_offset < size) { 62600d93d79Smatthias.ringwald 62700d93d79Smatthias.ringwald // handle signaling commands 62800d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 62900d93d79Smatthias.ringwald 63000d93d79Smatthias.ringwald // increment command_offset 63100d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 63200d93d79Smatthias.ringwald } 63300d93d79Smatthias.ringwald return; 63400d93d79Smatthias.ringwald } 63500d93d79Smatthias.ringwald 63600d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 63700d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 63800d93d79Smatthias.ringwald if (channel) { 63958de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 64000d93d79Smatthias.ringwald } 64100d93d79Smatthias.ringwald } 64200d93d79Smatthias.ringwald 64300d93d79Smatthias.ringwald 64427a923d0Smatthias.ringwald // finalize closed channel 64527a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 646f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 647f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 648f62db1e3Smatthias.ringwald 649f62db1e3Smatthias.ringwald // discard channel 650f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 651f62db1e3Smatthias.ringwald free (channel); 652c8e4258aSmatthias.ringwald } 6531e6aba47Smatthias.ringwald 6549d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 655c52bf64dSmatthias.ringwald linked_item_t *it; 6569d9bbc01Smatthias.ringwald 6579d9bbc01Smatthias.ringwald // close open channels 6589d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 6599d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 6609d9bbc01Smatthias.ringwald if ( service->psm == psm){ 6619d9bbc01Smatthias.ringwald return service; 6629d9bbc01Smatthias.ringwald }; 6639d9bbc01Smatthias.ringwald } 6649d9bbc01Smatthias.ringwald return NULL; 6659d9bbc01Smatthias.ringwald } 6669d9bbc01Smatthias.ringwald 667d8497f19Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 6689d9bbc01Smatthias.ringwald // check for alread registered psm // TODO: emit error event 6699d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 6709d9bbc01Smatthias.ringwald if (service) return; 6719d9bbc01Smatthias.ringwald 6729d9bbc01Smatthias.ringwald // alloc structure // TODO: emit error event 6739d9bbc01Smatthias.ringwald service = malloc(sizeof(l2cap_service_t)); 6749d9bbc01Smatthias.ringwald if (!service) return; 6759d9bbc01Smatthias.ringwald 6769d9bbc01Smatthias.ringwald // fill in 6779d9bbc01Smatthias.ringwald service->psm = psm; 6789d9bbc01Smatthias.ringwald service->mtu = mtu; 6799d9bbc01Smatthias.ringwald service->connection = connection; 680d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 6819d9bbc01Smatthias.ringwald 6829d9bbc01Smatthias.ringwald // add to services list 6839d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 6849d9bbc01Smatthias.ringwald } 6859d9bbc01Smatthias.ringwald 686116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){ 6879d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 688037d6e48Smatthias.ringwald if (!service) return; 6899d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 6909d9bbc01Smatthias.ringwald free(service); 6919d9bbc01Smatthias.ringwald } 6929d9bbc01Smatthias.ringwald 6939d9bbc01Smatthias.ringwald // 6949d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){ 6959d9bbc01Smatthias.ringwald linked_item_t *it; 6969d9bbc01Smatthias.ringwald 6979d9bbc01Smatthias.ringwald // close open channels 698c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 699c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 700c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 701c52bf64dSmatthias.ringwald if (channel->connection == connection) { 702c52bf64dSmatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 703b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 704c52bf64dSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 705c52bf64dSmatthias.ringwald } 706c52bf64dSmatthias.ringwald } 7079d9bbc01Smatthias.ringwald 708645658c9Smatthias.ringwald // unregister services 709*69025de8Smatthias.ringwald it = (linked_item_t *) &l2cap_services; 710*69025de8Smatthias.ringwald while (it->next){ 711*69025de8Smatthias.ringwald l2cap_service_t * service = (l2cap_service_t *) it->next; 712645658c9Smatthias.ringwald if (service->connection == connection){ 713*69025de8Smatthias.ringwald it->next = it->next->next; 7148149d2c7Smatthias.ringwald free(service); 7158149d2c7Smatthias.ringwald } else { 7168149d2c7Smatthias.ringwald it = it->next; 717645658c9Smatthias.ringwald } 718645658c9Smatthias.ringwald } 719c52bf64dSmatthias.ringwald } 720c52bf64dSmatthias.ringwald 7211e6aba47Smatthias.ringwald 7229edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){ 7239edc8742Smatthias.ringwald capture_connection = connection; 7249edc8742Smatthias.ringwald } 7251e6aba47Smatthias.ringwald 726b448a0e7Smatthias.ringwald