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 522784b77dSmatthias.ringwald // minimum signaling MTU 532784b77dSmatthias.ringwald #define L2CAP_MINIMAL_MTU 48 541dc511deSmatthias.ringwald #define L2CAP_DEFAULT_MTU 672 552784b77dSmatthias.ringwald 5600d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 5700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 5800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 5900d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6000d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6100d93d79Smatthias.ringwald 62*b502e1b0Smatthias.ringwald static void null_packet_handler(connection_t * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 632718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 64fcadd0caSmatthias.ringwald 651e6aba47Smatthias.ringwald static uint8_t * sig_buffer = NULL; 661e6aba47Smatthias.ringwald static linked_list_t l2cap_channels = NULL; 679d9bbc01Smatthias.ringwald static linked_list_t l2cap_services = NULL; 681e6aba47Smatthias.ringwald static uint8_t * acl_buffer = NULL; 69*b502e1b0Smatthias.ringwald static void (*packet_handler) (connection_t * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 709edc8742Smatthias.ringwald static connection_t * capture_connection = NULL; 711e6aba47Smatthias.ringwald 72169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 73169f8b28Smatthias.ringwald 741e6aba47Smatthias.ringwald void l2cap_init(){ 752784b77dSmatthias.ringwald sig_buffer = malloc( L2CAP_MINIMAL_MTU ); 762784b77dSmatthias.ringwald // acl_buffer = malloc( 255 + 8 ); 772784b77dSmatthias.ringwald acl_buffer = malloc( 400 + 8 ); 78fcadd0caSmatthias.ringwald 79fcadd0caSmatthias.ringwald // 802718e2e7Smatthias.ringwald // register callback with HCI 81fcadd0caSmatthias.ringwald // 822718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 83fcadd0caSmatthias.ringwald } 84fcadd0caSmatthias.ringwald 85fcadd0caSmatthias.ringwald 86fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 87*b502e1b0Smatthias.ringwald static void null_packet_handler(connection_t * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 88fcadd0caSmatthias.ringwald } 89*b502e1b0Smatthias.ringwald void l2cap_register_packet_handler(void (*handler)(connection_t * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 90*b502e1b0Smatthias.ringwald packet_handler = handler; 911e6aba47Smatthias.ringwald } 921e6aba47Smatthias.ringwald 9358de5610Smatthias.ringwald // notify client/protocol handler 9458de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 9558de5610Smatthias.ringwald if (channel->packet_handler) { 9658de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 9758de5610Smatthias.ringwald } else { 9858de5610Smatthias.ringwald switch (type){ 9958de5610Smatthias.ringwald case HCI_EVENT_PACKET: 100*b502e1b0Smatthias.ringwald (*packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, data, size); 10158de5610Smatthias.ringwald break; 10258de5610Smatthias.ringwald case L2CAP_DATA_PACKET: 103*b502e1b0Smatthias.ringwald (*packet_handler)(channel->connection, L2CAP_DATA_PACKET, channel->local_cid, data, size); 10458de5610Smatthias.ringwald break; 10558de5610Smatthias.ringwald default: 10658de5610Smatthias.ringwald // ?? 10758de5610Smatthias.ringwald break; 10858de5610Smatthias.ringwald } 10958de5610Smatthias.ringwald } 11058de5610Smatthias.ringwald } 11158de5610Smatthias.ringwald 11258de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 11358de5610Smatthias.ringwald uint8_t event[17]; 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); 12258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 12358de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 12458de5610Smatthias.ringwald } 12558de5610Smatthias.ringwald 12658de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 12758de5610Smatthias.ringwald uint8_t event[4]; 12858de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 12958de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 13058de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 13158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 13258de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 13358de5610Smatthias.ringwald } 13458de5610Smatthias.ringwald 13558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 13658de5610Smatthias.ringwald uint8_t event[16]; 13758de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 13858de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 13958de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 14058de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 14158de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 14258de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 14358de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 14458de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 14558de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1460af41d30Smatthias.ringwald } 1470af41d30Smatthias.ringwald 148b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 149f62db1e3Smatthias.ringwald linked_item_t *it; 150f62db1e3Smatthias.ringwald l2cap_channel_t * channel; 151f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 152f62db1e3Smatthias.ringwald channel = (l2cap_channel_t *) it; 153b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 154f62db1e3Smatthias.ringwald return channel; 155f62db1e3Smatthias.ringwald } 156f62db1e3Smatthias.ringwald } 157f62db1e3Smatthias.ringwald return NULL; 158f62db1e3Smatthias.ringwald } 159f62db1e3Smatthias.ringwald 16096cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 16196cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 16296cbd662Smatthias.ringwald if (channel) { 16396cbd662Smatthias.ringwald return channel->remote_mtu; 16496cbd662Smatthias.ringwald } 16596cbd662Smatthias.ringwald return 0; 16696cbd662Smatthias.ringwald } 16796cbd662Smatthias.ringwald 16858de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 16958de5610Smatthias.ringwald // printf("l2cap_send_signaling_packet type %u\n", cmd); 17058de5610Smatthias.ringwald va_list argptr; 17158de5610Smatthias.ringwald va_start(argptr, identifier); 17258de5610Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 17358de5610Smatthias.ringwald va_end(argptr); 17458de5610Smatthias.ringwald return hci_send_acl_packet(sig_buffer, len); 17558de5610Smatthias.ringwald } 17658de5610Smatthias.ringwald 17758de5610Smatthias.ringwald void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 17858de5610Smatthias.ringwald // find channel for local_cid, construct l2cap packet and send 17958de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 18058de5610Smatthias.ringwald if (channel) { 18158de5610Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 18258de5610Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 18358de5610Smatthias.ringwald // 2 - ACL length 18458de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 18558de5610Smatthias.ringwald // 4 - L2CAP packet length 18658de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 18758de5610Smatthias.ringwald // 6 - L2CAP channel DEST 18858de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 18958de5610Smatthias.ringwald // 8 - data 19058de5610Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 19158de5610Smatthias.ringwald // send 19258de5610Smatthias.ringwald hci_send_acl_packet(acl_buffer, len+8); 19358de5610Smatthias.ringwald } 19458de5610Smatthias.ringwald } 19558de5610Smatthias.ringwald 1961e6aba47Smatthias.ringwald // open outgoing L2CAP channel 1971e6aba47Smatthias.ringwald void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){ 1981e6aba47Smatthias.ringwald 1991e6aba47Smatthias.ringwald // alloc structure 2001e6aba47Smatthias.ringwald l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 2011e6aba47Smatthias.ringwald // TODO: emit error event 2021e6aba47Smatthias.ringwald if (!chan) return; 2031e6aba47Smatthias.ringwald 2041e6aba47Smatthias.ringwald // fill in 2051e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 2061e6aba47Smatthias.ringwald chan->psm = psm; 2071e6aba47Smatthias.ringwald chan->handle = 0; 2081e6aba47Smatthias.ringwald chan->connection = connection; 209b5a49eb1Smatthias.ringwald chan->packet_handler = NULL; 2102784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 2111e6aba47Smatthias.ringwald 2121e6aba47Smatthias.ringwald // set initial state 2131e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_CLOSED; 2141e6aba47Smatthias.ringwald chan->sig_id = L2CAP_SIG_ID_INVALID; 2151e6aba47Smatthias.ringwald 2161e6aba47Smatthias.ringwald // add to connections list 2171e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 2181e6aba47Smatthias.ringwald 2191e6aba47Smatthias.ringwald // send connection request 2201e6aba47Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 2211e6aba47Smatthias.ringwald hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 22243625864Smatthias.ringwald } 22343625864Smatthias.ringwald 224b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 225b35f641cSmatthias.ringwald // find channel for local_cid 226b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 227f62db1e3Smatthias.ringwald if (channel) { 228f62db1e3Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 229b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 230f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 231f62db1e3Smatthias.ringwald } 23243625864Smatthias.ringwald } 2331e6aba47Smatthias.ringwald 234afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 2351e6aba47Smatthias.ringwald linked_item_t *it; 2361e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 237b448a0e7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 238b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 239b448a0e7Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 240afde0c52Smatthias.ringwald // failure, forward error code 241afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 242afde0c52Smatthias.ringwald // discard channel 243afde0c52Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 244afde0c52Smatthias.ringwald free (channel); 245afde0c52Smatthias.ringwald } 246afde0c52Smatthias.ringwald } 247afde0c52Smatthias.ringwald } 248afde0c52Smatthias.ringwald } 249afde0c52Smatthias.ringwald 250afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 251afde0c52Smatthias.ringwald linked_item_t *it; 252afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 253afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 254afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 255afde0c52Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 256b448a0e7Smatthias.ringwald // success, start l2cap handshake 257afde0c52Smatthias.ringwald channel->handle = handle; 258b448a0e7Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 259b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 260b448a0e7Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 261b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid); 262afde0c52Smatthias.ringwald } 263afde0c52Smatthias.ringwald } 264afde0c52Smatthias.ringwald } 265afde0c52Smatthias.ringwald } 266b448a0e7Smatthias.ringwald 267afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 268afde0c52Smatthias.ringwald 269afde0c52Smatthias.ringwald bd_addr_t address; 270afde0c52Smatthias.ringwald hci_con_handle_t handle; 271afde0c52Smatthias.ringwald 272afde0c52Smatthias.ringwald switch(packet[0]){ 273afde0c52Smatthias.ringwald 274afde0c52Smatthias.ringwald // handle connection complete events 275afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 276afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 277afde0c52Smatthias.ringwald if (packet[2] == 0){ 278afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 279afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 280afde0c52Smatthias.ringwald } else { 281afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 282afde0c52Smatthias.ringwald } 283afde0c52Smatthias.ringwald break; 284afde0c52Smatthias.ringwald 285afde0c52Smatthias.ringwald // handle successful create connection cancel command 286afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 287afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 288afde0c52Smatthias.ringwald if (packet[5] == 0){ 289afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 290afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 291afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 29203cfbabcSmatthias.ringwald } 2931e6aba47Smatthias.ringwald } 294afde0c52Smatthias.ringwald break; 29527a923d0Smatthias.ringwald 2961e6aba47Smatthias.ringwald // handle disconnection complete events 297afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 29827a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 299afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 30027a923d0Smatthias.ringwald linked_item_t *it; 30139ce35a7Smatthias.ringwald // only access next element to allows for removal 30278a6f100Smatthias.ringwald for (it = (linked_item_t *) &l2cap_channels; it->next ; it = it->next){ 30339ce35a7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 30427a923d0Smatthias.ringwald if ( channel->handle == handle ){ 30539ce35a7Smatthias.ringwald // update prev item before free'ing next element 30639ce35a7Smatthias.ringwald it->next = it->next->next; 30727a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 30827a923d0Smatthias.ringwald } 30927a923d0Smatthias.ringwald } 310afde0c52Smatthias.ringwald break; 311fcadd0caSmatthias.ringwald 312ee091cf1Smatthias.ringwald // HCI Connection Timeouts 313afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 314afde0c52Smatthias.ringwald if (!capture_connection){ 315ee091cf1Smatthias.ringwald hci_con_handle_t handle = READ_BT_16(packet, 2); 316ee091cf1Smatthias.ringwald linked_item_t *it; 317ee091cf1Smatthias.ringwald l2cap_channel_t * channel; 318ee091cf1Smatthias.ringwald int used = 0; 319ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 320ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 321ee091cf1Smatthias.ringwald if (channel->handle == handle) { 322ee091cf1Smatthias.ringwald used = 1; 323ee091cf1Smatthias.ringwald } 324ee091cf1Smatthias.ringwald } 325ee091cf1Smatthias.ringwald if (!used) { 3269edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 327ee091cf1Smatthias.ringwald } 328ee091cf1Smatthias.ringwald } 329afde0c52Smatthias.ringwald break; 330ee091cf1Smatthias.ringwald 331afde0c52Smatthias.ringwald default: 332afde0c52Smatthias.ringwald break; 333afde0c52Smatthias.ringwald } 334afde0c52Smatthias.ringwald 335afde0c52Smatthias.ringwald // pass on 336*b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 3371e6aba47Smatthias.ringwald } 3381e6aba47Smatthias.ringwald 339afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 340f79ee244Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->local_cid, channel->remote_cid); 34184836b65Smatthias.ringwald l2cap_finialize_channel_close(channel); 34284836b65Smatthias.ringwald } 34384836b65Smatthias.ringwald 344b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 345645658c9Smatthias.ringwald 346b35f641cSmatthias.ringwald // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 347645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 348645658c9Smatthias.ringwald if (!service) { 349645658c9Smatthias.ringwald // 0x0002 PSM not supported 350169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm); 351645658c9Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0); 352645658c9Smatthias.ringwald return; 353645658c9Smatthias.ringwald } 354645658c9Smatthias.ringwald 355645658c9Smatthias.ringwald hci_connection_t * hci_connection = connection_for_handle( handle ); 356645658c9Smatthias.ringwald if (!hci_connection) { 357169f8b28Smatthias.ringwald fprintf(stderr, "no hci_connection for handle %u\n", handle); 358645658c9Smatthias.ringwald // TODO: emit error 359645658c9Smatthias.ringwald return; 360645658c9Smatthias.ringwald } 361645658c9Smatthias.ringwald // alloc structure 362169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request register channel\n"); 363169f8b28Smatthias.ringwald l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t)); 364645658c9Smatthias.ringwald // TODO: emit error event 365169f8b28Smatthias.ringwald if (!channel) return; 366645658c9Smatthias.ringwald 367645658c9Smatthias.ringwald // fill in 368169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 369169f8b28Smatthias.ringwald channel->psm = psm; 370169f8b28Smatthias.ringwald channel->handle = handle; 371169f8b28Smatthias.ringwald channel->connection = service->connection; 372f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 373b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 374b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 3752784b77dSmatthias.ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 376645658c9Smatthias.ringwald 377645658c9Smatthias.ringwald // set initial state 378e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 379e405ae81Smatthias.ringwald 380e405ae81Smatthias.ringwald // temp. store req sig id 381e405ae81Smatthias.ringwald channel->sig_id = sig_id; 382645658c9Smatthias.ringwald 383645658c9Smatthias.ringwald // add to connections list 384169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 385645658c9Smatthias.ringwald 386e405ae81Smatthias.ringwald // emit incoming connection request 387e405ae81Smatthias.ringwald l2cap_emit_connection_request(channel); 388e405ae81Smatthias.ringwald } 389645658c9Smatthias.ringwald 390b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 391b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 392e405ae81Smatthias.ringwald if (!channel) { 393b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 394e405ae81Smatthias.ringwald return; 395e405ae81Smatthias.ringwald } 396e405ae81Smatthias.ringwald 397e405ae81Smatthias.ringwald // accept connection 398b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0); 399e405ae81Smatthias.ringwald 400e405ae81Smatthias.ringwald // set real sig and state and start config 401e405ae81Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 402e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 403b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 404b35f641cSmatthias.ringwald 405b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 406e405ae81Smatthias.ringwald } 407645658c9Smatthias.ringwald 408b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 409b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 410e405ae81Smatthias.ringwald if (!channel) { 411b7de95daSmatthias.ringwald fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 412e405ae81Smatthias.ringwald return; 413e405ae81Smatthias.ringwald } 414e405ae81Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0); 415e405ae81Smatthias.ringwald 416e405ae81Smatthias.ringwald // discard channel 417e405ae81Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 418e405ae81Smatthias.ringwald free (channel); 419645658c9Smatthias.ringwald } 420645658c9Smatthias.ringwald 4212784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 4222784b77dSmatthias.ringwald // accept the other's configuration options 4233de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 4243de7c0caSmatthias.ringwald uint16_t pos = 8; 4253de7c0caSmatthias.ringwald while (pos < end_pos){ 4261dc511deSmatthias.ringwald uint8_t type = command[pos++]; 4271dc511deSmatthias.ringwald uint8_t length = command[pos++]; 4281dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 4291dc511deSmatthias.ringwald if ((type & 0x7f) == 1 && length == 2){ 4301dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 4313de7c0caSmatthias.ringwald // printf("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 4321dc511deSmatthias.ringwald } 4331dc511deSmatthias.ringwald pos += length; 4341dc511deSmatthias.ringwald } 4352784b77dSmatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 4361dc511deSmatthias.ringwald // send back received options 4371dc511deSmatthias.ringwald // l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 4381dc511deSmatthias.ringwald // send back OK 4391dc511deSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, 0, NULL); 4402784b77dSmatthias.ringwald } 4412784b77dSmatthias.ringwald 44200d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 4431e6aba47Smatthias.ringwald 44400d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 44500d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 44638e5900eSmatthias.ringwald uint16_t result = 0; 4471e6aba47Smatthias.ringwald 448b35f641cSmatthias.ringwald // printf("signaling handler code %u\n", code); 449b35f641cSmatthias.ringwald 4501e6aba47Smatthias.ringwald switch (channel->state) { 4511e6aba47Smatthias.ringwald 4521e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 4531e6aba47Smatthias.ringwald switch (code){ 4541e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 45500d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 45638e5900eSmatthias.ringwald switch (result) { 45738e5900eSmatthias.ringwald case 0: 458169f8b28Smatthias.ringwald // successful connection 45900d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 4601e6aba47Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 461b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 4625a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 46338e5900eSmatthias.ringwald break; 46438e5900eSmatthias.ringwald case 1: 46538e5900eSmatthias.ringwald // connection pending. get some coffee 46638e5900eSmatthias.ringwald break; 46738e5900eSmatthias.ringwald default: 468f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 46938e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 47038e5900eSmatthias.ringwald break; 4711e6aba47Smatthias.ringwald } 4721e6aba47Smatthias.ringwald break; 47338e5900eSmatthias.ringwald 47484836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 47584836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 47684836b65Smatthias.ringwald break; 47784836b65Smatthias.ringwald 47838e5900eSmatthias.ringwald default: 4791e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 48038e5900eSmatthias.ringwald break; 4811e6aba47Smatthias.ringwald } 4821e6aba47Smatthias.ringwald break; 4831e6aba47Smatthias.ringwald 4845a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 4851e6aba47Smatthias.ringwald switch (code) { 4861e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 4871e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 4881e6aba47Smatthias.ringwald break; 4895a67bd4aSmatthias.ringwald case CONFIGURE_REQUEST: 4902784b77dSmatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 4915a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 4925a67bd4aSmatthias.ringwald break; 49384836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 49484836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 49584836b65Smatthias.ringwald break; 4965a67bd4aSmatthias.ringwald default: 4975a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4985a67bd4aSmatthias.ringwald break; 4991e6aba47Smatthias.ringwald } 5001e6aba47Smatthias.ringwald break; 5011e6aba47Smatthias.ringwald 5021e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ: 5031e6aba47Smatthias.ringwald switch (code) { 5041e6aba47Smatthias.ringwald case CONFIGURE_REQUEST: 5052784b77dSmatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 5061e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 50703cfbabcSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 508c8e4258aSmatthias.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; 5155a67bd4aSmatthias.ringwald } 5165a67bd4aSmatthias.ringwald break; 5175a67bd4aSmatthias.ringwald 5185a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 5195a67bd4aSmatthias.ringwald switch (code) { 5205a67bd4aSmatthias.ringwald case CONFIGURE_RESPONSE: 5215a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 5225a67bd4aSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 5235a67bd4aSmatthias.ringwald break; 52484836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 52584836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 52684836b65Smatthias.ringwald break; 5275a67bd4aSmatthias.ringwald default: 5285a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 5295a67bd4aSmatthias.ringwald break; 530c8e4258aSmatthias.ringwald } 531c8e4258aSmatthias.ringwald break; 532f62db1e3Smatthias.ringwald 533f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 534f62db1e3Smatthias.ringwald switch (code) { 535f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 53627a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 53727a923d0Smatthias.ringwald break; 53884836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 53984836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 54084836b65Smatthias.ringwald break; 5415a67bd4aSmatthias.ringwald default: 5425a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 5435a67bd4aSmatthias.ringwald break; 54427a923d0Smatthias.ringwald } 54527a923d0Smatthias.ringwald break; 54684836b65Smatthias.ringwald 54784836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 54884836b65Smatthias.ringwald // @TODO handle incoming requests 54984836b65Smatthias.ringwald break; 55084836b65Smatthias.ringwald 55184836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 55284836b65Smatthias.ringwald switch (code) { 55384836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 55484836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 55584836b65Smatthias.ringwald break; 5565a67bd4aSmatthias.ringwald default: 55784836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 55884836b65Smatthias.ringwald break; 55984836b65Smatthias.ringwald } 5605a67bd4aSmatthias.ringwald break; 56110642e45Smatthias.ringwald default: 56210642e45Smatthias.ringwald break; 56327a923d0Smatthias.ringwald } 564b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 56527a923d0Smatthias.ringwald } 56627a923d0Smatthias.ringwald 56700d93d79Smatthias.ringwald 56800d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 56900d93d79Smatthias.ringwald 57000d93d79Smatthias.ringwald // get code, signalind identifier and command len 57100d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 57200d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 57300d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 57400d93d79Smatthias.ringwald 57500d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 57600d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 57700d93d79Smatthias.ringwald return; 57800d93d79Smatthias.ringwald } 57900d93d79Smatthias.ringwald 58000d93d79Smatthias.ringwald // general commands without an assigned channel 58100d93d79Smatthias.ringwald switch(code) { 58200d93d79Smatthias.ringwald 58300d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 58400d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 58500d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 58600d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 58700d93d79Smatthias.ringwald break; 58800d93d79Smatthias.ringwald } 58900d93d79Smatthias.ringwald 59000d93d79Smatthias.ringwald case ECHO_REQUEST: { 59100d93d79Smatthias.ringwald // send back packet 59200d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]); 59300d93d79Smatthias.ringwald break; 59400d93d79Smatthias.ringwald } 59500d93d79Smatthias.ringwald 59600d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 59700d93d79Smatthias.ringwald // we neither support connectionless L2CAP data nor support any flow control modes yet 59800d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 59900d93d79Smatthias.ringwald if (infoType == 2) { 60000d93d79Smatthias.ringwald uint32_t features = 0; 60100d93d79Smatthias.ringwald // extended features request supported, however no features present 60200d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 60300d93d79Smatthias.ringwald } else { 60400d93d79Smatthias.ringwald // all other types are not supported 60500d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 60600d93d79Smatthias.ringwald } 60700d93d79Smatthias.ringwald break;; 60800d93d79Smatthias.ringwald } 60900d93d79Smatthias.ringwald 61000d93d79Smatthias.ringwald default: 61100d93d79Smatthias.ringwald break; 61200d93d79Smatthias.ringwald } 61300d93d79Smatthias.ringwald 61400d93d79Smatthias.ringwald 61500d93d79Smatthias.ringwald // Get potential destination CID 61600d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 61700d93d79Smatthias.ringwald 61800d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 61900d93d79Smatthias.ringwald linked_item_t *it; 62000d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 62100d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 62200d93d79Smatthias.ringwald if (channel->handle == handle) { 62300d93d79Smatthias.ringwald if (code & 1) { 62400d93d79Smatthias.ringwald // match odd commands by previous signaling identifier 62500d93d79Smatthias.ringwald if (channel->sig_id == sig_id) { 62600d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 62700d93d79Smatthias.ringwald } 62800d93d79Smatthias.ringwald } else { 62900d93d79Smatthias.ringwald // match even commands by local channel id 63000d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 63100d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 63200d93d79Smatthias.ringwald } 63300d93d79Smatthias.ringwald } 63400d93d79Smatthias.ringwald } 63500d93d79Smatthias.ringwald } 63600d93d79Smatthias.ringwald } 63700d93d79Smatthias.ringwald 63800d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 63900d93d79Smatthias.ringwald 64000d93d79Smatthias.ringwald // Capturing? 64100d93d79Smatthias.ringwald if (capture_connection) { 642*b502e1b0Smatthias.ringwald (*packet_handler)(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size); 64300d93d79Smatthias.ringwald return; 64400d93d79Smatthias.ringwald } 64500d93d79Smatthias.ringwald 64600d93d79Smatthias.ringwald // Get Channel ID 64700d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 64800d93d79Smatthias.ringwald 64900d93d79Smatthias.ringwald // Signaling Packet? 65000d93d79Smatthias.ringwald if (channel_id == 1) { 65100d93d79Smatthias.ringwald 65200d93d79Smatthias.ringwald // Get Connection 65300d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 65400d93d79Smatthias.ringwald 65500d93d79Smatthias.ringwald uint16_t command_offset = 8; 65600d93d79Smatthias.ringwald while (command_offset < size) { 65700d93d79Smatthias.ringwald 65800d93d79Smatthias.ringwald // handle signaling commands 65900d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 66000d93d79Smatthias.ringwald 66100d93d79Smatthias.ringwald // increment command_offset 66200d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 66300d93d79Smatthias.ringwald } 66400d93d79Smatthias.ringwald return; 66500d93d79Smatthias.ringwald } 66600d93d79Smatthias.ringwald 66700d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 66800d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 66900d93d79Smatthias.ringwald if (channel) { 67058de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 67100d93d79Smatthias.ringwald } 67200d93d79Smatthias.ringwald } 67300d93d79Smatthias.ringwald 6742718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 6752718e2e7Smatthias.ringwald switch (packet_type) { 6762718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 6772718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 6782718e2e7Smatthias.ringwald break; 6792718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 6802718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 6812718e2e7Smatthias.ringwald break; 6822718e2e7Smatthias.ringwald default: 6832718e2e7Smatthias.ringwald break; 6842718e2e7Smatthias.ringwald } 6852718e2e7Smatthias.ringwald } 68600d93d79Smatthias.ringwald 68727a923d0Smatthias.ringwald // finalize closed channel 68827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 689f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 690f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 691f62db1e3Smatthias.ringwald 692f62db1e3Smatthias.ringwald // discard channel 693f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 694f62db1e3Smatthias.ringwald free (channel); 695c8e4258aSmatthias.ringwald } 6961e6aba47Smatthias.ringwald 6979d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 698c52bf64dSmatthias.ringwald linked_item_t *it; 6999d9bbc01Smatthias.ringwald 7009d9bbc01Smatthias.ringwald // close open channels 7019d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 7029d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 7039d9bbc01Smatthias.ringwald if ( service->psm == psm){ 7049d9bbc01Smatthias.ringwald return service; 7059d9bbc01Smatthias.ringwald }; 7069d9bbc01Smatthias.ringwald } 7079d9bbc01Smatthias.ringwald return NULL; 7089d9bbc01Smatthias.ringwald } 7099d9bbc01Smatthias.ringwald 710d8497f19Smatthias.ringwald void l2cap_register_service_internal(connection_t *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 7119d9bbc01Smatthias.ringwald // check for alread registered psm // TODO: emit error event 7129d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 7139d9bbc01Smatthias.ringwald if (service) return; 7149d9bbc01Smatthias.ringwald 7159d9bbc01Smatthias.ringwald // alloc structure // TODO: emit error event 7169d9bbc01Smatthias.ringwald service = malloc(sizeof(l2cap_service_t)); 7179d9bbc01Smatthias.ringwald if (!service) return; 7189d9bbc01Smatthias.ringwald 7199d9bbc01Smatthias.ringwald // fill in 7209d9bbc01Smatthias.ringwald service->psm = psm; 7219d9bbc01Smatthias.ringwald service->mtu = mtu; 7229d9bbc01Smatthias.ringwald service->connection = connection; 723d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 7249d9bbc01Smatthias.ringwald 7259d9bbc01Smatthias.ringwald // add to services list 7269d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 7279d9bbc01Smatthias.ringwald } 7289d9bbc01Smatthias.ringwald 729116ee617Smatthias.ringwald void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){ 7309d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 731037d6e48Smatthias.ringwald if (!service) return; 7329d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 7339d9bbc01Smatthias.ringwald free(service); 7349d9bbc01Smatthias.ringwald } 7359d9bbc01Smatthias.ringwald 7369d9bbc01Smatthias.ringwald // 7379d9bbc01Smatthias.ringwald void l2cap_close_connection(connection_t *connection){ 7389d9bbc01Smatthias.ringwald linked_item_t *it; 7399d9bbc01Smatthias.ringwald 7409d9bbc01Smatthias.ringwald // close open channels 741c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 742c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 743c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 744c52bf64dSmatthias.ringwald if (channel->connection == connection) { 745c52bf64dSmatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 746b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 747c52bf64dSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 748c52bf64dSmatthias.ringwald } 749c52bf64dSmatthias.ringwald } 7509d9bbc01Smatthias.ringwald 751645658c9Smatthias.ringwald // unregister services 75269025de8Smatthias.ringwald it = (linked_item_t *) &l2cap_services; 75369025de8Smatthias.ringwald while (it->next){ 75469025de8Smatthias.ringwald l2cap_service_t * service = (l2cap_service_t *) it->next; 755645658c9Smatthias.ringwald if (service->connection == connection){ 75669025de8Smatthias.ringwald it->next = it->next->next; 7578149d2c7Smatthias.ringwald free(service); 7588149d2c7Smatthias.ringwald } else { 7598149d2c7Smatthias.ringwald it = it->next; 760645658c9Smatthias.ringwald } 761645658c9Smatthias.ringwald } 762c52bf64dSmatthias.ringwald } 763c52bf64dSmatthias.ringwald 7641e6aba47Smatthias.ringwald 7659edc8742Smatthias.ringwald void l2cap_set_capture_connection(connection_t * connection){ 7669edc8742Smatthias.ringwald capture_connection = connection; 7679edc8742Smatthias.ringwald } 7681e6aba47Smatthias.ringwald 769b448a0e7Smatthias.ringwald