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 6236944dffSmatthias.ringwald static void null_packet_handler(void * 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; 6936944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 701e6aba47Smatthias.ringwald 71169f8b28Smatthias.ringwald static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 72169f8b28Smatthias.ringwald 731e6aba47Smatthias.ringwald void l2cap_init(){ 742784b77dSmatthias.ringwald sig_buffer = malloc( L2CAP_MINIMAL_MTU ); 75*11c41d51Smatthias.ringwald acl_buffer = malloc( HCI_ACL_3DH5_SIZE); 76fcadd0caSmatthias.ringwald 77fcadd0caSmatthias.ringwald // 782718e2e7Smatthias.ringwald // register callback with HCI 79fcadd0caSmatthias.ringwald // 802718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 81fcadd0caSmatthias.ringwald } 82fcadd0caSmatthias.ringwald 83fcadd0caSmatthias.ringwald 84fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 8536944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 86fcadd0caSmatthias.ringwald } 8736944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 88b502e1b0Smatthias.ringwald packet_handler = handler; 891e6aba47Smatthias.ringwald } 901e6aba47Smatthias.ringwald 9158de5610Smatthias.ringwald // notify client/protocol handler 9258de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 9358de5610Smatthias.ringwald if (channel->packet_handler) { 9458de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 9558de5610Smatthias.ringwald } else { 9636944dffSmatthias.ringwald (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 9758de5610Smatthias.ringwald } 9858de5610Smatthias.ringwald } 9958de5610Smatthias.ringwald 10058de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 10158de5610Smatthias.ringwald uint8_t event[17]; 10258de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 10358de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 10458de5610Smatthias.ringwald event[2] = status; 10558de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 10658de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 10758de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 10858de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 10958de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 11058de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 11158de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 11258de5610Smatthias.ringwald } 11358de5610Smatthias.ringwald 11458de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 11558de5610Smatthias.ringwald uint8_t event[4]; 11658de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 11758de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 11858de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 11958de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 12058de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 12158de5610Smatthias.ringwald } 12258de5610Smatthias.ringwald 12358de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 12458de5610Smatthias.ringwald uint8_t event[16]; 12558de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 12658de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 12758de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 12858de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 12958de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 13058de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 13158de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 13258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 13358de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1340af41d30Smatthias.ringwald } 1350af41d30Smatthias.ringwald 136b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 137f62db1e3Smatthias.ringwald linked_item_t *it; 138f62db1e3Smatthias.ringwald l2cap_channel_t * channel; 139f62db1e3Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 140f62db1e3Smatthias.ringwald channel = (l2cap_channel_t *) it; 141b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 142f62db1e3Smatthias.ringwald return channel; 143f62db1e3Smatthias.ringwald } 144f62db1e3Smatthias.ringwald } 145f62db1e3Smatthias.ringwald return NULL; 146f62db1e3Smatthias.ringwald } 147f62db1e3Smatthias.ringwald 14896cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 14996cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 15096cbd662Smatthias.ringwald if (channel) { 15196cbd662Smatthias.ringwald return channel->remote_mtu; 15296cbd662Smatthias.ringwald } 15396cbd662Smatthias.ringwald return 0; 15496cbd662Smatthias.ringwald } 15596cbd662Smatthias.ringwald 15658de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 15758de5610Smatthias.ringwald // printf("l2cap_send_signaling_packet type %u\n", cmd); 15858de5610Smatthias.ringwald va_list argptr; 15958de5610Smatthias.ringwald va_start(argptr, identifier); 16058de5610Smatthias.ringwald uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 16158de5610Smatthias.ringwald va_end(argptr); 16258de5610Smatthias.ringwald return hci_send_acl_packet(sig_buffer, len); 16358de5610Smatthias.ringwald } 16458de5610Smatthias.ringwald 16558de5610Smatthias.ringwald void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 16658de5610Smatthias.ringwald // find channel for local_cid, construct l2cap packet and send 16758de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 16858de5610Smatthias.ringwald if (channel) { 16958de5610Smatthias.ringwald // 0 - Connection handle : PB=10 : BC=00 17058de5610Smatthias.ringwald bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 17158de5610Smatthias.ringwald // 2 - ACL length 17258de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 17358de5610Smatthias.ringwald // 4 - L2CAP packet length 17458de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 17558de5610Smatthias.ringwald // 6 - L2CAP channel DEST 17658de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 17758de5610Smatthias.ringwald // 8 - data 17858de5610Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 17958de5610Smatthias.ringwald // send 18058de5610Smatthias.ringwald hci_send_acl_packet(acl_buffer, len+8); 18158de5610Smatthias.ringwald } 18258de5610Smatthias.ringwald } 18358de5610Smatthias.ringwald 1841e6aba47Smatthias.ringwald // open outgoing L2CAP channel 1856b296a27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm){ 1861e6aba47Smatthias.ringwald 1871e6aba47Smatthias.ringwald // alloc structure 1881e6aba47Smatthias.ringwald l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 1891e6aba47Smatthias.ringwald // TODO: emit error event 1901e6aba47Smatthias.ringwald if (!chan) return; 1911e6aba47Smatthias.ringwald 1921e6aba47Smatthias.ringwald // fill in 1931e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 1941e6aba47Smatthias.ringwald chan->psm = psm; 1951e6aba47Smatthias.ringwald chan->handle = 0; 1961e6aba47Smatthias.ringwald chan->connection = connection; 1976b296a27Smatthias.ringwald chan->packet_handler = packet_handler; 1982784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 1991e6aba47Smatthias.ringwald 2001e6aba47Smatthias.ringwald // set initial state 2011e6aba47Smatthias.ringwald chan->state = L2CAP_STATE_CLOSED; 2021e6aba47Smatthias.ringwald chan->sig_id = L2CAP_SIG_ID_INVALID; 2031e6aba47Smatthias.ringwald 2041e6aba47Smatthias.ringwald // add to connections list 2051e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 2061e6aba47Smatthias.ringwald 2071e6aba47Smatthias.ringwald // send connection request 2081e6aba47Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 2091e6aba47Smatthias.ringwald hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 21043625864Smatthias.ringwald } 21143625864Smatthias.ringwald 212b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 213b35f641cSmatthias.ringwald // find channel for local_cid 214b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 215f62db1e3Smatthias.ringwald if (channel) { 216f62db1e3Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 217b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 218f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 219f62db1e3Smatthias.ringwald } 22043625864Smatthias.ringwald } 2211e6aba47Smatthias.ringwald 222afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 2231e6aba47Smatthias.ringwald linked_item_t *it; 2241e6aba47Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 225b448a0e7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 226b448a0e7Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 227b448a0e7Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 228afde0c52Smatthias.ringwald // failure, forward error code 229afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 230afde0c52Smatthias.ringwald // discard channel 231afde0c52Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 232afde0c52Smatthias.ringwald free (channel); 233afde0c52Smatthias.ringwald } 234afde0c52Smatthias.ringwald } 235afde0c52Smatthias.ringwald } 236afde0c52Smatthias.ringwald } 237afde0c52Smatthias.ringwald 238afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 239afde0c52Smatthias.ringwald linked_item_t *it; 240afde0c52Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 241afde0c52Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 242afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 243afde0c52Smatthias.ringwald if (channel->state == L2CAP_STATE_CLOSED) { 244b448a0e7Smatthias.ringwald // success, start l2cap handshake 245afde0c52Smatthias.ringwald channel->handle = handle; 246b448a0e7Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 247b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 248b448a0e7Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 249b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid); 250afde0c52Smatthias.ringwald } 251afde0c52Smatthias.ringwald } 252afde0c52Smatthias.ringwald } 253afde0c52Smatthias.ringwald } 254b448a0e7Smatthias.ringwald 255afde0c52Smatthias.ringwald void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 256afde0c52Smatthias.ringwald 257afde0c52Smatthias.ringwald bd_addr_t address; 258afde0c52Smatthias.ringwald hci_con_handle_t handle; 25936944dffSmatthias.ringwald linked_item_t *it; 260afde0c52Smatthias.ringwald 261afde0c52Smatthias.ringwald switch(packet[0]){ 262afde0c52Smatthias.ringwald 263afde0c52Smatthias.ringwald // handle connection complete events 264afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 265afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 266afde0c52Smatthias.ringwald if (packet[2] == 0){ 267afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 268afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 269afde0c52Smatthias.ringwald } else { 270afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 271afde0c52Smatthias.ringwald } 272afde0c52Smatthias.ringwald break; 273afde0c52Smatthias.ringwald 274afde0c52Smatthias.ringwald // handle successful create connection cancel command 275afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 276afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 277afde0c52Smatthias.ringwald if (packet[5] == 0){ 278afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 279afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 280afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 28103cfbabcSmatthias.ringwald } 2821e6aba47Smatthias.ringwald } 283afde0c52Smatthias.ringwald break; 28427a923d0Smatthias.ringwald 2851e6aba47Smatthias.ringwald // handle disconnection complete events 286afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 28727a923d0Smatthias.ringwald // send l2cap disconnect events for all channels on this handle 288afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 28939ce35a7Smatthias.ringwald // only access next element to allows for removal 29078a6f100Smatthias.ringwald for (it = (linked_item_t *) &l2cap_channels; it->next ; it = it->next){ 29139ce35a7Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 29227a923d0Smatthias.ringwald if ( channel->handle == handle ){ 29339ce35a7Smatthias.ringwald // update prev item before free'ing next element 29439ce35a7Smatthias.ringwald it->next = it->next->next; 29527a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 29627a923d0Smatthias.ringwald } 29727a923d0Smatthias.ringwald } 298afde0c52Smatthias.ringwald break; 299fcadd0caSmatthias.ringwald 300ee091cf1Smatthias.ringwald // HCI Connection Timeouts 301afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 30236944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 303ee091cf1Smatthias.ringwald l2cap_channel_t * channel; 304ee091cf1Smatthias.ringwald int used = 0; 305ee091cf1Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 306ee091cf1Smatthias.ringwald channel = (l2cap_channel_t *) it; 307ee091cf1Smatthias.ringwald if (channel->handle == handle) { 308ee091cf1Smatthias.ringwald used = 1; 309ee091cf1Smatthias.ringwald } 310ee091cf1Smatthias.ringwald } 311ee091cf1Smatthias.ringwald if (!used) { 3129edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 313ee091cf1Smatthias.ringwald } 314afde0c52Smatthias.ringwald break; 315ee091cf1Smatthias.ringwald 316afde0c52Smatthias.ringwald default: 317afde0c52Smatthias.ringwald break; 318afde0c52Smatthias.ringwald } 319afde0c52Smatthias.ringwald 320afde0c52Smatthias.ringwald // pass on 321b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 3221e6aba47Smatthias.ringwald } 3231e6aba47Smatthias.ringwald 324afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 325f79ee244Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->local_cid, channel->remote_cid); 32684836b65Smatthias.ringwald l2cap_finialize_channel_close(channel); 32784836b65Smatthias.ringwald } 32884836b65Smatthias.ringwald 329b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 330645658c9Smatthias.ringwald 331b35f641cSmatthias.ringwald // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 332645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 333645658c9Smatthias.ringwald if (!service) { 334645658c9Smatthias.ringwald // 0x0002 PSM not supported 335169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm); 336645658c9Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0); 337645658c9Smatthias.ringwald return; 338645658c9Smatthias.ringwald } 339645658c9Smatthias.ringwald 340645658c9Smatthias.ringwald hci_connection_t * hci_connection = connection_for_handle( handle ); 341645658c9Smatthias.ringwald if (!hci_connection) { 342169f8b28Smatthias.ringwald fprintf(stderr, "no hci_connection for handle %u\n", handle); 343645658c9Smatthias.ringwald // TODO: emit error 344645658c9Smatthias.ringwald return; 345645658c9Smatthias.ringwald } 346645658c9Smatthias.ringwald // alloc structure 347169f8b28Smatthias.ringwald // printf("l2cap_handle_connection_request register channel\n"); 348169f8b28Smatthias.ringwald l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t)); 349645658c9Smatthias.ringwald // TODO: emit error event 350169f8b28Smatthias.ringwald if (!channel) return; 351645658c9Smatthias.ringwald 352645658c9Smatthias.ringwald // fill in 353169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 354169f8b28Smatthias.ringwald channel->psm = psm; 355169f8b28Smatthias.ringwald channel->handle = handle; 356169f8b28Smatthias.ringwald channel->connection = service->connection; 357f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 358b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 359b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 3602784b77dSmatthias.ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 361645658c9Smatthias.ringwald 362645658c9Smatthias.ringwald // set initial state 363e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 364e405ae81Smatthias.ringwald 365e405ae81Smatthias.ringwald // temp. store req sig id 366e405ae81Smatthias.ringwald channel->sig_id = sig_id; 367645658c9Smatthias.ringwald 368645658c9Smatthias.ringwald // add to connections list 369169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 370645658c9Smatthias.ringwald 371e405ae81Smatthias.ringwald // emit incoming connection request 372e405ae81Smatthias.ringwald l2cap_emit_connection_request(channel); 373e405ae81Smatthias.ringwald } 374645658c9Smatthias.ringwald 375b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 376b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 377e405ae81Smatthias.ringwald if (!channel) { 378b35f641cSmatthias.ringwald fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 379e405ae81Smatthias.ringwald return; 380e405ae81Smatthias.ringwald } 381e405ae81Smatthias.ringwald 382e405ae81Smatthias.ringwald // accept connection 383b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0); 384e405ae81Smatthias.ringwald 385e405ae81Smatthias.ringwald // set real sig and state and start config 386e405ae81Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 387e405ae81Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 388b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 389b35f641cSmatthias.ringwald 390b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 391e405ae81Smatthias.ringwald } 392645658c9Smatthias.ringwald 393b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 394b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 395e405ae81Smatthias.ringwald if (!channel) { 396b7de95daSmatthias.ringwald fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 397e405ae81Smatthias.ringwald return; 398e405ae81Smatthias.ringwald } 399e405ae81Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0); 400e405ae81Smatthias.ringwald 401e405ae81Smatthias.ringwald // discard channel 402e405ae81Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 403e405ae81Smatthias.ringwald free (channel); 404645658c9Smatthias.ringwald } 405645658c9Smatthias.ringwald 4062784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 4072784b77dSmatthias.ringwald // accept the other's configuration options 4083de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 4093de7c0caSmatthias.ringwald uint16_t pos = 8; 4103de7c0caSmatthias.ringwald while (pos < end_pos){ 4111dc511deSmatthias.ringwald uint8_t type = command[pos++]; 4121dc511deSmatthias.ringwald uint8_t length = command[pos++]; 4131dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 4141dc511deSmatthias.ringwald if ((type & 0x7f) == 1 && length == 2){ 4151dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 4163de7c0caSmatthias.ringwald // printf("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 4171dc511deSmatthias.ringwald } 4181dc511deSmatthias.ringwald pos += length; 4191dc511deSmatthias.ringwald } 4202784b77dSmatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 4211dc511deSmatthias.ringwald // send back received options 4221dc511deSmatthias.ringwald // l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 4231dc511deSmatthias.ringwald // send back OK 4241dc511deSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, 0, NULL); 4252784b77dSmatthias.ringwald } 4262784b77dSmatthias.ringwald 42700d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 4281e6aba47Smatthias.ringwald 42900d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 43000d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 43138e5900eSmatthias.ringwald uint16_t result = 0; 4321e6aba47Smatthias.ringwald 433b35f641cSmatthias.ringwald // printf("signaling handler code %u\n", code); 434b35f641cSmatthias.ringwald 4351e6aba47Smatthias.ringwald switch (channel->state) { 4361e6aba47Smatthias.ringwald 4371e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 4381e6aba47Smatthias.ringwald switch (code){ 4391e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 44000d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 44138e5900eSmatthias.ringwald switch (result) { 44238e5900eSmatthias.ringwald case 0: 443169f8b28Smatthias.ringwald // successful connection 44400d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 4451e6aba47Smatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 446b35f641cSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 4475a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 44838e5900eSmatthias.ringwald break; 44938e5900eSmatthias.ringwald case 1: 45038e5900eSmatthias.ringwald // connection pending. get some coffee 45138e5900eSmatthias.ringwald break; 45238e5900eSmatthias.ringwald default: 453f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 45438e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 45538e5900eSmatthias.ringwald break; 4561e6aba47Smatthias.ringwald } 4571e6aba47Smatthias.ringwald break; 45838e5900eSmatthias.ringwald 45984836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 46084836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 46184836b65Smatthias.ringwald break; 46284836b65Smatthias.ringwald 46338e5900eSmatthias.ringwald default: 4641e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 46538e5900eSmatthias.ringwald break; 4661e6aba47Smatthias.ringwald } 4671e6aba47Smatthias.ringwald break; 4681e6aba47Smatthias.ringwald 4695a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 4701e6aba47Smatthias.ringwald switch (code) { 4711e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 4721e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 4731e6aba47Smatthias.ringwald break; 4745a67bd4aSmatthias.ringwald case CONFIGURE_REQUEST: 4752784b77dSmatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 4765a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 4775a67bd4aSmatthias.ringwald break; 47884836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 47984836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 48084836b65Smatthias.ringwald break; 4815a67bd4aSmatthias.ringwald default: 4825a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4835a67bd4aSmatthias.ringwald break; 4841e6aba47Smatthias.ringwald } 4851e6aba47Smatthias.ringwald break; 4861e6aba47Smatthias.ringwald 4871e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ: 4881e6aba47Smatthias.ringwald switch (code) { 4891e6aba47Smatthias.ringwald case CONFIGURE_REQUEST: 4902784b77dSmatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 4911e6aba47Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 49203cfbabcSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 493c8e4258aSmatthias.ringwald break; 49484836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 49584836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 49684836b65Smatthias.ringwald break; 4975a67bd4aSmatthias.ringwald default: 4985a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 4995a67bd4aSmatthias.ringwald break; 5005a67bd4aSmatthias.ringwald } 5015a67bd4aSmatthias.ringwald break; 5025a67bd4aSmatthias.ringwald 5035a67bd4aSmatthias.ringwald case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 5045a67bd4aSmatthias.ringwald switch (code) { 5055a67bd4aSmatthias.ringwald case CONFIGURE_RESPONSE: 5065a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 5075a67bd4aSmatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 5085a67bd4aSmatthias.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; 515c8e4258aSmatthias.ringwald } 516c8e4258aSmatthias.ringwald break; 517f62db1e3Smatthias.ringwald 518f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 519f62db1e3Smatthias.ringwald switch (code) { 520f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 52127a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 52227a923d0Smatthias.ringwald break; 52384836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 52484836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 52584836b65Smatthias.ringwald break; 5265a67bd4aSmatthias.ringwald default: 5275a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 5285a67bd4aSmatthias.ringwald break; 52927a923d0Smatthias.ringwald } 53027a923d0Smatthias.ringwald break; 53184836b65Smatthias.ringwald 53284836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 53384836b65Smatthias.ringwald // @TODO handle incoming requests 53484836b65Smatthias.ringwald break; 53584836b65Smatthias.ringwald 53684836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 53784836b65Smatthias.ringwald switch (code) { 53884836b65Smatthias.ringwald case DISCONNECTION_REQUEST: 53984836b65Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 54084836b65Smatthias.ringwald break; 5415a67bd4aSmatthias.ringwald default: 54284836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 54384836b65Smatthias.ringwald break; 54484836b65Smatthias.ringwald } 5455a67bd4aSmatthias.ringwald break; 54610642e45Smatthias.ringwald default: 54710642e45Smatthias.ringwald break; 54827a923d0Smatthias.ringwald } 549b35f641cSmatthias.ringwald // printf("new state %u\n", channel->state); 55027a923d0Smatthias.ringwald } 55127a923d0Smatthias.ringwald 55200d93d79Smatthias.ringwald 55300d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 55400d93d79Smatthias.ringwald 55500d93d79Smatthias.ringwald // get code, signalind identifier and command len 55600d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 55700d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 55800d93d79Smatthias.ringwald uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 55900d93d79Smatthias.ringwald 56000d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 56100d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 56200d93d79Smatthias.ringwald return; 56300d93d79Smatthias.ringwald } 56400d93d79Smatthias.ringwald 56500d93d79Smatthias.ringwald // general commands without an assigned channel 56600d93d79Smatthias.ringwald switch(code) { 56700d93d79Smatthias.ringwald 56800d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 56900d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 57000d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 57100d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 57200d93d79Smatthias.ringwald break; 57300d93d79Smatthias.ringwald } 57400d93d79Smatthias.ringwald 57500d93d79Smatthias.ringwald case ECHO_REQUEST: { 57600d93d79Smatthias.ringwald // send back packet 57700d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]); 57800d93d79Smatthias.ringwald break; 57900d93d79Smatthias.ringwald } 58000d93d79Smatthias.ringwald 58100d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 58200d93d79Smatthias.ringwald // we neither support connectionless L2CAP data nor support any flow control modes yet 58300d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 58400d93d79Smatthias.ringwald if (infoType == 2) { 58500d93d79Smatthias.ringwald uint32_t features = 0; 58600d93d79Smatthias.ringwald // extended features request supported, however no features present 58700d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 58800d93d79Smatthias.ringwald } else { 58900d93d79Smatthias.ringwald // all other types are not supported 59000d93d79Smatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 59100d93d79Smatthias.ringwald } 59200d93d79Smatthias.ringwald break;; 59300d93d79Smatthias.ringwald } 59400d93d79Smatthias.ringwald 59500d93d79Smatthias.ringwald default: 59600d93d79Smatthias.ringwald break; 59700d93d79Smatthias.ringwald } 59800d93d79Smatthias.ringwald 59900d93d79Smatthias.ringwald 60000d93d79Smatthias.ringwald // Get potential destination CID 60100d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 60200d93d79Smatthias.ringwald 60300d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 60400d93d79Smatthias.ringwald linked_item_t *it; 60500d93d79Smatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 60600d93d79Smatthias.ringwald l2cap_channel_t * channel = (l2cap_channel_t *) it; 60700d93d79Smatthias.ringwald if (channel->handle == handle) { 60800d93d79Smatthias.ringwald if (code & 1) { 60900d93d79Smatthias.ringwald // match odd commands by previous signaling identifier 61000d93d79Smatthias.ringwald if (channel->sig_id == sig_id) { 61100d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 61200d93d79Smatthias.ringwald } 61300d93d79Smatthias.ringwald } else { 61400d93d79Smatthias.ringwald // match even commands by local channel id 61500d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 61600d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 61700d93d79Smatthias.ringwald } 61800d93d79Smatthias.ringwald } 61900d93d79Smatthias.ringwald } 62000d93d79Smatthias.ringwald } 62100d93d79Smatthias.ringwald } 62200d93d79Smatthias.ringwald 62300d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 62400d93d79Smatthias.ringwald 62500d93d79Smatthias.ringwald // Get Channel ID 62600d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 62700d93d79Smatthias.ringwald 62800d93d79Smatthias.ringwald // Signaling Packet? 62900d93d79Smatthias.ringwald if (channel_id == 1) { 63000d93d79Smatthias.ringwald 63100d93d79Smatthias.ringwald // Get Connection 63200d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 63300d93d79Smatthias.ringwald 63400d93d79Smatthias.ringwald uint16_t command_offset = 8; 63500d93d79Smatthias.ringwald while (command_offset < size) { 63600d93d79Smatthias.ringwald 63700d93d79Smatthias.ringwald // handle signaling commands 63800d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 63900d93d79Smatthias.ringwald 64000d93d79Smatthias.ringwald // increment command_offset 64100d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 64200d93d79Smatthias.ringwald } 64300d93d79Smatthias.ringwald return; 64400d93d79Smatthias.ringwald } 64500d93d79Smatthias.ringwald 64600d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 64700d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 64800d93d79Smatthias.ringwald if (channel) { 64958de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 65000d93d79Smatthias.ringwald } 65100d93d79Smatthias.ringwald } 65200d93d79Smatthias.ringwald 6532718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 6542718e2e7Smatthias.ringwald switch (packet_type) { 6552718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 6562718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 6572718e2e7Smatthias.ringwald break; 6582718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 6592718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 6602718e2e7Smatthias.ringwald break; 6612718e2e7Smatthias.ringwald default: 6622718e2e7Smatthias.ringwald break; 6632718e2e7Smatthias.ringwald } 6642718e2e7Smatthias.ringwald } 66500d93d79Smatthias.ringwald 66627a923d0Smatthias.ringwald // finalize closed channel 66727a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 668f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 669f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 670f62db1e3Smatthias.ringwald 671f62db1e3Smatthias.ringwald // discard channel 672f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 673f62db1e3Smatthias.ringwald free (channel); 674c8e4258aSmatthias.ringwald } 6751e6aba47Smatthias.ringwald 6769d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 677c52bf64dSmatthias.ringwald linked_item_t *it; 6789d9bbc01Smatthias.ringwald 6799d9bbc01Smatthias.ringwald // close open channels 6809d9bbc01Smatthias.ringwald for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 6819d9bbc01Smatthias.ringwald l2cap_service_t * service = ((l2cap_service_t *) it); 6829d9bbc01Smatthias.ringwald if ( service->psm == psm){ 6839d9bbc01Smatthias.ringwald return service; 6849d9bbc01Smatthias.ringwald }; 6859d9bbc01Smatthias.ringwald } 6869d9bbc01Smatthias.ringwald return NULL; 6879d9bbc01Smatthias.ringwald } 6889d9bbc01Smatthias.ringwald 68936944dffSmatthias.ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 6909d9bbc01Smatthias.ringwald // check for alread registered psm // TODO: emit error event 6919d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 6929d9bbc01Smatthias.ringwald if (service) return; 6939d9bbc01Smatthias.ringwald 6949d9bbc01Smatthias.ringwald // alloc structure // TODO: emit error event 6959d9bbc01Smatthias.ringwald service = malloc(sizeof(l2cap_service_t)); 6969d9bbc01Smatthias.ringwald if (!service) return; 6979d9bbc01Smatthias.ringwald 6989d9bbc01Smatthias.ringwald // fill in 6999d9bbc01Smatthias.ringwald service->psm = psm; 7009d9bbc01Smatthias.ringwald service->mtu = mtu; 7019d9bbc01Smatthias.ringwald service->connection = connection; 702d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 7039d9bbc01Smatthias.ringwald 7049d9bbc01Smatthias.ringwald // add to services list 7059d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 7069d9bbc01Smatthias.ringwald } 7079d9bbc01Smatthias.ringwald 70836944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 7099d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 710037d6e48Smatthias.ringwald if (!service) return; 7119d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 7129d9bbc01Smatthias.ringwald free(service); 7139d9bbc01Smatthias.ringwald } 7149d9bbc01Smatthias.ringwald 7159d9bbc01Smatthias.ringwald // 71636944dffSmatthias.ringwald void l2cap_close_connection(void *connection){ 7179d9bbc01Smatthias.ringwald linked_item_t *it; 7189d9bbc01Smatthias.ringwald 7199d9bbc01Smatthias.ringwald // close open channels 720c52bf64dSmatthias.ringwald l2cap_channel_t * channel; 721c52bf64dSmatthias.ringwald for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 722c52bf64dSmatthias.ringwald channel = (l2cap_channel_t *) it; 723c52bf64dSmatthias.ringwald if (channel->connection == connection) { 724c52bf64dSmatthias.ringwald channel->sig_id = l2cap_next_sig_id(); 725b35f641cSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 726c52bf64dSmatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 727c52bf64dSmatthias.ringwald } 728c52bf64dSmatthias.ringwald } 7299d9bbc01Smatthias.ringwald 730645658c9Smatthias.ringwald // unregister services 73169025de8Smatthias.ringwald it = (linked_item_t *) &l2cap_services; 73269025de8Smatthias.ringwald while (it->next){ 73369025de8Smatthias.ringwald l2cap_service_t * service = (l2cap_service_t *) it->next; 734645658c9Smatthias.ringwald if (service->connection == connection){ 73569025de8Smatthias.ringwald it->next = it->next->next; 7368149d2c7Smatthias.ringwald free(service); 7378149d2c7Smatthias.ringwald } else { 7388149d2c7Smatthias.ringwald it = it->next; 739645658c9Smatthias.ringwald } 740645658c9Smatthias.ringwald } 741c52bf64dSmatthias.ringwald } 742c52bf64dSmatthias.ringwald